Last active
June 21, 2018 16:25
-
-
Save jptillman/ba3f29fd82cc83f3f1de0d807b50c843 to your computer and use it in GitHub Desktop.
Drupal8 module for sync'ing profile pic with CiviCRM contact pic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use Drupal\Core\Routing\RouteMatchInterface; | |
| function civipicsync_get_user_picture_path($account) { | |
| $pic = $account->user_picture; | |
| $path = NULL; | |
| if (!$pic->isEmpty()) { | |
| $path = drupal_realpath($pic->entity->getFileUri()); | |
| } | |
| return $path; | |
| } | |
| function civipicsync_user_update($account) { | |
| $picturePath = civipicsync_get_user_picture_path($account); | |
| $oldPicturePath = civipicsync_get_user_picture_path($account->original); | |
| // No change means nothing to do here | |
| if ($picturePath == $oldPicturePath) return; | |
| try { | |
| $civicrm = \Drupal::service('civicrm'); | |
| $civicrm->initialize(); | |
| } | |
| catch (Exception $e) { | |
| \Drupal::logger('civipicsync')->error( | |
| 'Error when initializing CiviCRM: @errorMessage', | |
| array( | |
| '@errorMessage' => $e->getMessage() | |
| ) | |
| ); | |
| return; | |
| } | |
| $matchResult = NULL; | |
| try { | |
| $matchResult = civicrm_api3('UFMatch', 'get', array( | |
| 'uf_id' => $account->id(), | |
| 'contact_type' => 'Individual' | |
| )); | |
| // Zero matches is not an error, as not all Drupal users must | |
| // have a matching Civi contact record | |
| if (count($matchResult['values']) == 0) return; | |
| } | |
| catch (CiviCRM_API3_Exception $e) { | |
| \Drupal::logger('civipicsync')->error( | |
| 'Error when finding CiviCRM contact matching Drupal user ' . | |
| ' %username (uid %uid): @errorMessage', | |
| array( | |
| '%username' => $account->getUsername(), | |
| '%uid' => $account->id(), | |
| '@errorMessage' => $e->getMessage() | |
| ) | |
| ); | |
| return; | |
| } | |
| $firstResult = array_values($matchResult['values'])[0]; | |
| $cid = $firstResult['contact_id']; | |
| $config = CRM_Core_Config::singleton(); | |
| # NULL won't work here, as Civi's api won't recognize that we are deleting it | |
| $imageUrl = ''; | |
| if ($picturePath) { | |
| $newFileName = CRM_Utils_File::makeFileName($picturePath); | |
| $civiPicturePath = $config->customFileUploadDir . $newFileName; | |
| copy($picturePath, $civiPicturePath); | |
| // This translated into the proper URL via the call to processImageParams() usage below | |
| $imageUrl = [ | |
| 'name' => $civiPicturePath, | |
| 'type' => $account->user_picture->entity->getMimeType(), | |
| ]; | |
| } | |
| $params = array( | |
| 'id' => $cid, | |
| 'image_URL' => $imageUrl, | |
| ); | |
| // This thanks to https://civicrm.stackexchange.com/users/155/pradeep-nayak | |
| // Automatically calculates the http://www.mydrupalsite.com/civicrm/contact/imagefile?photo=FILENAME URL | |
| CRM_Contact_BAO_Contact::processImageParams($params); | |
| try { | |
| $result = civicrm_api3('Contact', 'create', $params); | |
| \Drupal::logger('civipicsync')->notice('Synchronized Drupal profile picture ' . | |
| 'for user %username with CiviCRM contact %civiname (id %cid)', | |
| array( | |
| '%username' => $account->getUsername(), | |
| '%civiname' => $result['values'][$cid]['display_name'], | |
| '%cid' => $cid | |
| ) | |
| ); | |
| } | |
| catch (CiviCRM_API3_Exception $e) { | |
| \Drupal::logger('civipicsync')->error( | |
| 'Error when synchronizing Drupal profile picture for user %username with ' . | |
| 'CiviCRM contact id %cid: @errorMessage', | |
| array( | |
| '%username' => $account->getUsername(), | |
| '%cid' => $params['id'], | |
| '@errorMessage' => $e->getMessage() | |
| ) | |
| ); | |
| return; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The basic approach here is to: