Skip to content

Commit

Permalink
Add hook_user_(login|insert|update|delete) functions.
Browse files Browse the repository at this point in the history
User registration profile fields are now pushed through to CiviCRM.
  • Loading branch information
torrance committed May 25, 2014
1 parent cbc32ef commit 29b7571
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
2 changes: 2 additions & 0 deletions civicrm.module
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

define('CIVICRM_UF_HEAD', TRUE);

require_once 'civicrm.user.inc';

/**
* Implements hook_permission().
*/
Expand Down
54 changes: 54 additions & 0 deletions civicrm.user.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

use \Drupal\Core\Session\AccountInterface;

/**
* Implements hook_user_login().
*/
function civicrm_user_login(AccountInterface $account) {
\Drupal::service('civicrm')->synchronizeUser($account);
}

/**
* Implements hook_user_insert().
*/
function civicrm_user_insert(AccountInterface $account) {
$civicrm = \Drupal::service('civicrm');
$civicrm->synchronizeUser($account);

// As per CRM-7858, the email address in CiviCRM isn't always set
// with a call to synchronize(). So we force this through.
$contact_id = \CRM_Core_BAO_UFMatch::getContactId($account->id());
\CRM_Core_BAO_UFMatch::updateContactEmail($contact_id, $account->getEmail());

// Process any profile form fields that may have been submitted.
// In particular, this will pick up form fields that were submitted on the user_registration page.
\CRM_Core_BAO_UFGroup::getEditHTML($contact_id, '', 2, TRUE, FALSE, NULL, FALSE, $civicrm->getCtype());
}

/**
* Implements hook_user_update().
*/
function civicrm_user_update(AccountInterface $account) {
\Drupal::service('civicrm');

// Update primary email address of contact if it has changed.
$contact_id = \CRM_Core_BAO_UFMatch::getContactId($account->id());
if ($contact_id) {
$contact_email = \CRM_Contact_BAO_Contact::getPrimaryEmail($contact_id);
if ($contact_email != $account->getEmail()) {
\CRM_Core_BAO_UFMatch::updateContactEmail($contact_id, $account->getEmail());
}
}

// @Todo Drupal 7 code cleared navigation menu if it detected a change in roles.
// \CRM_Core_BAO_Navigation::resetNavigation($contact_id);
}

/**
* Implements hook_user_delete().
*/
function civicrm_user_delete(AccountInterface $account) {
\Drupal::service('civicrm');
\CRM_Core_BAO_UFMatch::deleteUser($account->id());
}
2 changes: 1 addition & 1 deletion src/Civicrm.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function synchronizeUser(AccountInterface $account, $contact_type = 'Indi
*
* @Todo: Document what this function is doing and why.
*/
protected function getCtype($default = 'Individual') {
public function getCtype($default = 'Individual') {
if (!empty($_REQUEST['ctype'])) {
$ctype = $_REQUEST['ctype'];
}
Expand Down

0 comments on commit 29b7571

Please sign in to comment.