Közzétéve
2009-06-14 08:15
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);
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?