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) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Making sense of hooks
Pages: [1]

Author Topic: Making sense of hooks  (Read 1297 times)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Making sense of hooks
April 18, 2011, 05:00:47 pm
I was working through a fairly simple task. When a pledge is entered, calculate a custom field on that pledges based on the total & one other custom field. However, I found that doing this through a hook caused a minefield of gotchas:

First up I tried writing a postProcess hook. I got the hook working testing on editing the pledge but I found the same hook didn't work for when they created the pledge (I gave up on this ages ago so I can't remember the details but I think the data prevented was different.

So, I concluded that exposing using jquery to pre-fill the custom field would cover both fields & would look prettier than fixing it on save. But then they came back & said that front end data entry (via the contribution form) wasn't being saved. I realised that there could potentally also be data going in by code so I decided the only way to be sure was to focus on when the pledge is created.

So, I tried the pre hook - no use there - the pledge isn't created yet.

OK, the post hook. I didn't have all the data available that I needed so I did a pledge_get & a pledge_create via the api but the custom field I set was lost each time. It turned out that although the pledge object had been committed to the DB the corresponding custom field commit was still about to happen so any change to custom fields made at this point gets overwritten

So, I tried the custom hook. This finally worked. Yes, I had to do a pledge_get & pledge_create to get the data but there was no other point in the process where I felt I reliably had this data so that was the same as anywhere else. My feeling is that I am going to increasingly focus on using the post & custom hooks because I'm not confident that when I use a form hook I'll catch all the ways data could be coming in.  The gotch here is to avoid a loop - set a static variable.

Not sure there are any useful lessons from this except - however long you think it will take to customise Civi - tripple your estimate!
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Making sense of hooks
April 18, 2011, 11:17:52 pm
Can you do something with the new logging? I use hooks to set custom fields for contacts (to keep two databases synchronized) and it works, but figuring out when stuff is saved between the pre hook and the postProcess hook took some doing, and I retrieve data from the civicrm DB too which I expected to have in one of the hooks. If I had to do the same thing with the current setup I might have a go at basing the code on the logging functionality.
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Making sense of hooks
April 19, 2011, 12:13:29 am
IMO, something useful would be to provide an access to the context, eg.when you are in a contact create hook, knowing if it's because an admin creates it normally, through a profile, if it's because that's an event registration, if it's because you create an org because the current employer of the individual registering for the event...

Beside avoiding Eileen hammering the db to retrieve data that are already around, can allow to add a lot of intelligence to the system and act differently based on the context...

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Making sense of hooks
April 19, 2011, 12:25:22 am
I think the main problem I had with the form hooks was that they seemed to be ... wait for it .. form-oriented - so when more than one form might acheive a task (like creating a pledge) I needed to write the hook in 3 different ways. Also, the front end form only held the fields that were in the profile for the pledge (not the ones that I wanted to check / set).

I think I am actually going to go with a cron that sets them after the fact....
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Making sense of hooks
April 19, 2011, 06:29:04 am
In general if i'm manipulating a column in the DB, i prefer to use the post hook on that object (which for custom groups is the custom hook), primarily because there are many ways that object can be updated and hence form hooks are more limited

In general the hook system is not the most efficient system, but does provide a cleaner and simpler way of modifying the flow / data

do agree about context. would be good to incorporate some of larry's ideas from butler / drupal 8. check:

http://drupal.org/project/butler

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Making sense of hooks
April 19, 2011, 07:54:40 am
For the pre/post hook beside the context issue (going to read about butler), the difficulty is that not all the entities are using them.

Do you think it's realistic to remove them in the various BAOs and change the BAO so it's called 100% of the time for 4.0.1 ?
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Making sense of hooks
April 19, 2011, 10:14:43 am

this is a fairly big change and we'd prefer to do it in 4.1 and earlier in the development cycle rather than later

The pre hook still needs to be called from the BAO. The post hook can be called from the DAO, and we might want to do it selectively for the 20-30 tables that need it (rather than all 134 tables, since its a loop thru the module list, checking for a function existence in D6)

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Making sense of hooks
April 19, 2011, 02:12:43 pm
If it's to be done selectively it would be good to have a way to allow the site administrators / developers define / over-ride the selection - perhaps in a similar way to how drupal does views prefixing - defining an array in the settings file.

At the moment I have a need for the post hooks in contributionRecur & pledgePayment. I submitted a patch but of course if you disagree then I'll have to nurse it through the various release cycles. This isn't great for me but it also seems it would be easier for you if I could make that decision independently on my install without you having to worry how it affects others.

If we implemented this in some way then all the BAO would need to call a function that checks whehter they should implement the pre hooks (i.e. the function would be in every BAO but the hook calling would be done selectively)
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Making sense of hooks

This forum was archived on 2017-11-26.