Automatic addition of Drupal account upon admin membership creation

Veröffentlicht
2010-02-12 14:11
Written by
illmasterc - member of the CiviCRM community - view blog guidelines
This is by no means an elegant solution, but is at least a step towards automatically creating an associated Drupal user when adding a membership to a CiviCRM individual. The code basically grabs the email from the contactId, sets the necessary paramaters to create the Drupal user, and then calls the method that does the creation. I was getting a form error for a while that was not allowing the creation of the new Drupal. I eventually troubleshot the error down to there being no password set in the form when creating the Drupal user. I'm not sure my workaround is the best, but I used the hook_form_alter to make password not required, and it worked for me. Here is the code below: function MODULENAME_civicrm_postProcess( $formName, &$form ){ if($form->_BAOName == "CRM_Member_BAO_Membership" && $form->_action == 1){ //creating new membership require_once("CRM/Contribute/BAO/Contribution/Utils.php"); require_once('CRM/Contact/BAO/Contact.php'); $email = CRM_Contact_BAO_Contact::getPrimaryEmail($form->_contactID); $params = array('cms_create_account' => TRUE, 'cms_name' => $email, 'email-5' => $email); CRM_Contribute_BAO_Contribution_Utils::createCMSUser( $params, $form->_contactID,'email-5'); } } function MODULENAME_form_alter(&$form, $form_state, $form_id){ //remove the password necessity when autocreating drupal users during admin member creation if($form_id == 'user_register'){ $form['pass']['#required'] = FALSE; } }
Filed under

Comments

Instead of using CRM_Contact_BAO_Contact::getPrimaryEmail I might've used civicrm_contact_get() and instead of CRM_Contribute_BAO_Contribution_Utils::createCMSUser I might've used user_add() (which would solve your password problem) and I think it will tie it to the existing contact and not create a new one.

Anonymous (nicht überprüft)
2010-02-14 - 20:25

Several people face the same situation. One solution that came up was to use the LoginToboggan module which can opt-out of a password requirement and create a temporary ACL role. I haven't tried it, but we are definitely going to need this soon - thanks for the tip!

so having worked on the above, can you see a quick route to re-using some of that code to provide a button on the contact summary screen so that if there is no CMS user, there can be an 'add cms account' button done performs the required action?

/*
* Automatically creates drupal user from membership form in admin
* Requires the auto password generation module as password is required
* http://drupal.org/project/genpass
* (don't forget to configure auto-generation via admin/user/settings)
*/

function MODULENAME_civicrm_postProcess($formName, &$form ){
if ($formName == "CRM_Member_Form_Membership" && $form->getAction() == 1){ //creating new membership
require_once("CRM/Contribute/BAO/Contribution/Utils.php");
require_once('CRM/Contact/BAO/Contact.php');
$cid = $form->getVar('_contactID');
$email = CRM_Contact_BAO_Contact::getPrimaryEmail($cid);
$params = array('cms_create_account' => TRUE, 'cms_name' => $email, 'email' => $email);
CRM_Contribute_BAO_Contribution_Utils::createCMSUser( $params, $cid,'email');
}
}