Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // In my extension it was good enough to hardcode in a constant. If desirable, one or more special group
- // IDs could be stored in a CiviCRM setting.
- define('EXAMPLEEXTENSION_GROUP_ID', 44);
- /**
- * Implements hook_civicrm_post().
- */
- function exampleextension_civicrm_post($op, $objectName, $groupId, &$contactIds) {
- if ($op === 'create' && $objectName === 'GroupContact' && $groupId == EXAMPLEEXTENSION_GROUP_ID) {
- foreach ($contactIds as $cid) {
- try {
- $email = civicrm_api3('Contact', 'getvalue', array(
- 'id' => $cid,
- 'return' => 'email',
- ));
- } catch (Exception $e) {
- continue;
- }
- // it's theoretically possible that a Drupal user exists with the given email
- // address but that it doesn't have a UFJoin, so we'll check for the existence
- // of the actual account
- if (!user_load_by_mail($email)) {
- $drupalUser = _ee_createAccount($email);
- if ($drupalUser) {
- $operation = 'register_no_approval_required';
- _user_mail_notify($operation, $drupalUser);
- }
- }
- }
- }
- }
- /**
- * Creates a Drupal user.
- *
- * Assumes that the nonexistence of the Drupal user has already been validated.
- *
- * @param string $email
- * @return mixed
- * Drupal user object if a new user was created,
- * FALSE if creation failed
- */
- function _ee_createAccount($email) {
- $result = FALSE;
- $params = array(
- 'init' => $email,
- 'name' => $email,
- 'mail' => $email,
- 'pass' => md5(uniqid(rand(), true)),
- 'status' => 1,
- );
- $result = user_save(NULL, $params);
- return $result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement