CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • Implementing Custom Hook on insert/update/delete of addresses
Pages: [1]

Author Topic: Implementing Custom Hook on insert/update/delete of addresses  (Read 537 times)

sggreener

  • I post occasionally
  • **
  • Posts: 35
  • Karma: 0
    • The SpatialDB Advisor
  • CiviCRM version: 4.6.15
  • CMS version: Joomla 5.5.1
  • MySQL version: 5.6.29
  • PHP version: 5.3.8
Implementing Custom Hook on insert/update/delete of addresses
May 03, 2016, 01:50:40 am
Folks,

Not sure what forum to post this. And please humor my lack of expertise or terminology.

The situation I am in is that I have a custom field XXX associated with a CiviCrm address field.
The custom field's value is derived from a CviCRM address field via a special lookup table (loaded into MySQL).
An example might be something that associates a ZIP code with another value (max installations per zip?).

So, when a user inserts/updates/deletes a value for that custom field, I need to write a Hook that detects the change to the address field, executes a SQL statement against the loaded lookup table, and assigns the returned value to the custom field.

As a database person, this could be encoded in a trigger on the civicrm_addresses table. The problem is, as you all know, MySQL only allows one trigger per table, and so any changes I might make manually to that trigger might be lost with an upgrade to the system.

So, it seems that I need to implement some sort of Hook.

I have searched through the documentation and examples, and I can't find a fully worked example that shows me how to code the Hook and how to deploy it (Joomla/Civcrm).

It must be a reasonably common problem but I can't find (my Google Coding is poor) a fully worked example showing me how to do this.

Can anyone help/guide me on this?

regards
Simon


JonGold

  • Ask me questions
  • ****
  • Posts: 638
  • Karma: 81
    • Palante Technology
  • CiviCRM version: 4.1 to the latest
  • CMS version: Drupal 6-7, Wordpress 4.0+
  • PHP version: PHP 5.3-5.5
Re: Implementing Custom Hook on insert/update/delete of addresses
May 03, 2016, 05:51:31 am
Hi there,

The best "forum" for this is now Stack Exchange:  http://civicrm.stackexchange.com.

First, I wanted to make sure you were aware of the "region lookup" extension, which does more or less what you want.  Even if it doesn't meet your needs, as a code example it's valuable: https://civicrm.org/extensions/civicrm-region-lookup

Second - you're right that it's possible to use a hook to add a trigger - but this is less common than handling the change in PHP.  PHP is simpler, though if you have high volume, the trigger will give better performance.

Either way - the first thing you'll want to do is get civix (https://github.com/totten/civix) up and running.  The easiest way to do that is via civicrm-buildkit (https://buildkit.civicrm.org/).  There's a presentation today at the NYC CiviCRM Meetup on buildkit using Vagrant, so if you like Vagrant, my guess is that the civicrm-buildkit Vagrantfile is mature now.

Once you have civix up, run "civix generate:module org.mydomain.mymodulename", and it auto-generates an extension for you.  Almost there!  Make sure that extension is enabled in your CiviCRM installation.

Now, you have a choice: use triggers or PHP.

If you use triggers, you need to define mymodulename_civicrm_trigger_info.  There's an example that sounds exactly like what you're trying to do here: https://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_trigger_info

If you use PHP, you can use either hook_civicrm_pre or hook_civicrm_post.  hook_civicrm_post is a little easier to use - but I can't recall if editing a custom field on an address triggers hook_civicrm_post for an address, leaving you in a potential endless loop situation.  I think you're fine.

I don't have a good example of hook_civicrm_post on addresses on my Github, but this is a really good example of hook_civicrm_post: https://github.com/PalanteJon/coop.palantetech.module.automaticsoftcredit/blob/master/automaticsoftcredit.php#L184

Hook references are documented here: https://wiki.civicrm.org/confluence/display/CRMDOC/Hook+Reference
Sign up to StackExchange and get free expert CiviCRM advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

sggreener

  • I post occasionally
  • **
  • Posts: 35
  • Karma: 0
    • The SpatialDB Advisor
  • CiviCRM version: 4.6.15
  • CMS version: Joomla 5.5.1
  • MySQL version: 5.6.29
  • PHP version: 5.3.8
Re: Implementing Custom Hook on insert/update/delete of addresses
May 03, 2016, 04:22:57 pm
Thanks for the quick reply and the detail.

I will follow up your suggestions and report back.

regards
Simon

sggreener

  • I post occasionally
  • **
  • Posts: 35
  • Karma: 0
    • The SpatialDB Advisor
  • CiviCRM version: 4.6.15
  • CMS version: Joomla 5.5.1
  • MySQL version: 5.6.29
  • PHP version: 5.3.8
Re: Implementing Custom Hook on insert/update/delete of addresses
May 04, 2016, 11:09:31 pm
John,

Thanks for the information. I've had a quick look. There is a lot of technology here that I am unfamiliar with.

Just to be clear, I don't need a PRE hook just a POST hook. If I was doing it in a trigger I would use an AFTER INSERT/UPDATE FOR EACH ROW trigger.

I need to grab the final geo_code_1/geo_code_2 from civicrm_addresses insert/update and query a table I have loaded. On getting a resulting value, I then write it to a custom field civicrm_value_* table setting the appropriate field value. Deletes are easier as the table is handled by civi as it is a civicrm_value_* object implementing a custom field.

So there is no chance of getting an insert/update look on civicrm_addresses.

My inclination is to modify the existing CiviCRM trigger (as I am a database guru of many years standing) but all the work can be lost if a trigger hook is deployed or an upgrade to the system occurs.

Simple is better!

Simon

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • Implementing Custom Hook on insert/update/delete of addresses

This forum was archived on 2017-11-26.