Support desk blog: Using CiviCRM hooks and API to accomplish certain tasks ...

Published
2009-06-14 08:15
Written by
Over the past few releases we've been drinking a fair amount of the hook kool-aid. This has proven to be super effective and gets around the problem of hacking the core files, but still allow a developer to extend the functionality of the system in minor/major ways. We've used hooks also to implement event discounts and contribution/event tracking. This week on IRC we had a few more questions which were effectively solved using hooks.

How do you delete a contact when a drupal user is deleted

Tom (http://www.kirkdesigns.co.uk/) wanted to delete the civicrm contact if the drupal user was deleted. The solution was to implement hook_user that gets called before civicrm's hook_user (you can do this by ensuring that your module has a lower weight than civicrm.module). Use the api call civicrm_uf_id_get to get the contact id for a given drupal user id. This is followed by an api call to civicrm_contact_delete to delete the contact.

How do you register a user in a third party system

Tom also wanted to create an account in a third party system via SOAP and store the value in a custom field. The solution was to implement hook_user that gets called AFTER civicrm's hook_user. The code in that hook is:
      $dsn = call_third_party_function();
      
      civicrm_initialize( );
      require_once 'CRM/Core/BAO/UFMatch.php';
      require_once 'CRM/Core/BAO/CustomValueTable.php';
      
      $contactID = CRM_Core_BAO_UFMatch::getContactId( $account->uid );
      $params = array( 'entityID' => $contactID, 'custom_14' =>  $dsn);
      CRM_Core_BAO_CustomValueTable::setValues($params);

How do I modify the search results to show columns of my choosing

Chris Ivens (http://joltbox.co.uk) wanted to modify the search results to show columns different from the default (name/address/phone/email). There is a little used feature in CiviCRM that allows you to display search results via a profile. In advanced search you can choose the profile from the select box (upper right corner). You can also send the profile via URL: http://drupal.demo.civicrm.org/civicrm/contact/search?reset=1&force=1&id=1 The profile fields displayed have to have "public visibility" and be "in result set". Eileen pointed out in the forums that, the profile is used for contacts of all types (org/ind/hh), whereas a mixed profile cannot be created. Would be cool to make this a configurable variable in the database, so folks can change their default search results column. Ping us on IRC if you are interested in working on this
Filed under

Comments

For #2 I often instead use hook_civicrm_post and then search to see if a uid exists for the contact. That way you don't have to worry about module weights.

It's great to see that there is a solution for this. Can you put the details in easy to follow steps?