3

I'm trying to find a way to automatically update the CiviCRM contact image when a user updates their Drupal profile image. I'm specifically looking for Drupal8.5/Civi4.7 but I'll take anything that can properly update Civi4.7 at this point, regardless of Drupal version. It seems that the Drupal/Civi API functions are almost, but not quite, capable of making this easy, although it may just be a documentation deficiency -- I'm still digging in the code.

I found this discussion on the old Civi forum (circa 2011) which discusses it. https://forum.civicrm.org/index.php%3Ftopic=22405.0.html

There is also some old code in the Civi site which directly updates the database and also doesn't seem to work anymore: https://wiki.civicrm.org/confluence/display/CRMDOC/Adding+pictures+in+civicrm+profile+page

Is there anything as of Civi 4.7.30 which can do this?

If not, can someone point me to some docs/examples that demonstrate how to programmatically add a photo image to a contact in Civi?

4

I am not sure how to sync drupal profile image with CiviCRM contact image through UI. But you can add module and invoke hook to update the contact image.

Here is the snippet how to add image url to contact

$params = [
  'contact_id' => 2,
  'image_URL' => [
    'name' => '/var/www/html/drupal/sites/default/files/civicrm/custom/Screenshot_from_2018_06_19_15_46_08_1877c5451ed2e68a12816071e0006fef.png',
    'type' => 'image/png',
  ],
];
CRM_Contact_BAO_Contact::processImageParams($params);
civicrm_api3('Contact', 'create', $params);

HTH

Pradeep

  • This is incorrect, at least for Civi 4.7.30. The way you provide the image_URL above, it will get persisted in the database field as "Array" (the literal word) and be useless. Even if you provide the full filesystem path string for image_URL, this will still not work, because it is not considered a valid URL by the Civi code. image_URL has to be a specially formatted FULLY QUALIFIED URL (!?) to work or it will simply be displayed as "Custom" in the display and you won't see the image. – theta-fish Jun 21 '18 at 13:13
  • Did you try the above snippet or you guessing it as IT WILL NEVER WORK!! – Pradeep Nayak Jun 21 '18 at 13:24
  • Actually, yes, I did. The record ended up with 'Array' in the field, as I stated. Also, you obviously never ran it, because there is a syntax error in the last line of code. – theta-fish Jun 21 '18 at 15:33
  • Well, drat, I stand corrected. In fixing your typo in the last line, I apparently accidentally removed the call to processImageParams() which I see now converts the filesystem path to the appropriate URL. That'll teach me to make sure I've correctly run the provided example before critiquing. – theta-fish Jun 21 '18 at 15:44
  • I've incorporate your processImageParams() approach into the gist I linked to in my answer. Your helpful response removed some complexity and hard-coded values. I appreciate your assistance. – theta-fish Jun 21 '18 at 16:15
1

I think I've figured this out and it seems to work reliably in D8.5.4/CV4.7.30.

The basic approach is to:

  1. Lookup the Civi contact based on the Drupal user id
  2. Make a copy of the uploaded account pic (Civi can't share, and requires the file to be in the custom uploads dir)
  3. Update the image_URL of the Civi contact using the Civi v3 api

You MUST use the image_URL value expected by the Civi code, which is specific to its function. It looks like this...

http://www.mydrupalsite.com/civicrm/contact/imagefile?photo=FILENAME.jpg

...and the file must be in the CiviCRM custom uploads folder. I see no alternatives to this in the code and the docs are completely silent on it. And, yes, I'm aware that this is a FULLY QUALIFIED URL being stored in the database. Whoever chose this technique was either highly constrained by legacy code or smoking crack. I've tried relative URLs and they don't seem to work.

I've created a Github gist here that demonstrates the approach (use at your own risk):

https://gist.github.com/jptillman/ba3f29fd82cc83f3f1de0d807b50c843

In my site using this code, removing the Drupal image also removes the Civi image from the Contact record, but I'm fairly sure that Civi isn't cleaning up, so this will leave orphaned files behind in the custom uploads folder. Probably wouldn't be too hard to engineer a delete of the file somehow.

NOTE: This code will overwrite any existing picture in the Civi record.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.