From 3eb59ab5798fef14aa0c922a7cd875ea8253f5c7 Mon Sep 17 00:00:00 2001 From: Allen Shaw Date: Wed, 3 Feb 2016 14:45:03 -0600 Subject: [PATCH] CRM_17609: Correct user->contact sync in Drupal 8. --- CRM/Core/BAO/UFMatch.php | 23 +++++++-------- CRM/Utils/System/Drupal8.php | 55 ++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 11 deletions(-) diff --git a/CRM/Core/BAO/UFMatch.php b/CRM/Core/BAO/UFMatch.php index 151252ce81..b8b5901ef7 100644 --- a/CRM/Core/BAO/UFMatch.php +++ b/CRM/Core/BAO/UFMatch.php @@ -254,18 +254,19 @@ AND domain_id = %2 } if (!$found) { - if ($config->userSystem->is_drupal) { - $mail = 'mail'; - } - elseif ($uf == 'WordPress') { - $mail = 'user_email'; - } - else { - $mail = 'email'; - } - + // Not sure why we're testing for this. Is there ever a case + // in which $user is not an object? if (is_object($user)) { - $params = array('email-Primary' => $user->$mail); + if ($config->userSystem->is_drupal) { + $primary_email = $uniqId; + } + elseif ($uf == 'WordPress') { + $primary_email = $user->user_email; + } + else { + $primary_email = $user->email; + } + $params = array('email-Primary' => $primary_email); } if ($ctype == 'Organization') { diff --git a/CRM/Utils/System/Drupal8.php b/CRM/Utils/System/Drupal8.php index 09f95cd0d5..56085e95e6 100644 --- a/CRM/Utils/System/Drupal8.php +++ b/CRM/Utils/System/Drupal8.php @@ -562,4 +562,59 @@ class CRM_Utils_System_Drupal8 extends CRM_Utils_System_DrupalBase { return $modules; } + /** + * @inheritDoc + */ + public function getUniqueIdentifierFromUserObject($user) { + return $user->get('mail')->value; + } + + /** + * @inheritDoc + */ + public function getUserIDFromUserObject($user) { + return $user->get('uid')->value; + } + + /** + * @inheritDoc + */ + public function synchronizeUsers() { + $config = CRM_Core_Config::singleton(); + if (PHP_SAPI != 'cli') { + set_time_limit(300); + } + + $users = array(); + $users = \Drupal::entityTypeManager()->getStorage('user')->loadByProperties(); + + $uf = $config->userFramework; + $contactCount = 0; + $contactCreated = 0; + $contactMatching = 0; + foreach ($users as $user) { + $mail = $user->get('mail')->value; + if (empty($mail)) { + continue; + } + $uid = $user->get('uid')->value; + $contactCount++; + if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $uid, $mail, $uf, 1, 'Individual', TRUE)) { + $contactCreated++; + } + else { + $contactMatching++; + } + if (is_object($match)) { + $match->free(); + } + } + + return array( + 'contactCount' => $contactCount, + 'contactMatching' => $contactMatching, + 'contactCreated' => $contactCreated, + ); + } + } -- 2.25.1