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) »
  • [Solved] hook_civicrm_custom and hook_civicrm_post defeated by form?
Pages: [1]

Author Topic: [Solved] hook_civicrm_custom and hook_civicrm_post defeated by form?  (Read 834 times)

sdragnev

  • I post occasionally
  • **
  • Posts: 40
  • Karma: 0
  • CiviCRM version: 4.6.2
  • CMS version: Drupal 7
  • PHP version: 5.4
[Solved] hook_civicrm_custom and hook_civicrm_post defeated by form?
March 28, 2013, 08:30:57 am
Hi there,

I'm using 4.2.6 and trying to set a contact's custom field value based on their address, but it seems like both hook_civicrm_custom and hook_civicrm_post run BEFORE the form is saved.

Workflow:

Edit contact
Changed address
Hit Save

What happens:
My code in hook_civicrm_post (or hook_civicrm_custom, based on $groupID and $op == 'edit') runs, calls the API to update the custom field which then calls CRM_Core_BAO_CustomValueTable::setValues with the values I give it. The custom field is now updated to what I want it.
CRM_Core_BAO_CustomValueTable::setValues runs *again* and sets the values to what the form has.

I've tried increasing my module's weight to be greater than CiviCRM but that achieved nothing.
Shouldn't the form update run after the post hooks?

The only other solution I see is to use hook_civicrm_postProcess or jquery/ajax to update the custom field while the form is open and once the address changes.
I may have luck with postProcess but this seems like a bug to me.

Thanks.
« Last Edit: April 02, 2013, 07:34:41 am by llaen »

andrewhunt

  • I post occasionally
  • **
  • Posts: 80
  • Karma: 13
    • AGH Strategies
  • CiviCRM version: all of 'em
  • CMS version: Drupal, Joomla, and WordPress
Re: hook_civicrm_custom and hook_civicrm_post defeated by form?
March 28, 2013, 09:00:24 am
In effect, the post hook(s) will get called before the postprocess hook.  The form's postprocess function will save everything it needs to save, and then it'll trigger the postprocess hooks.  hook_civicrm_pre and hook_civicrm_post run, respectively, before and after the actual saving of data, which happens along the way through the postprocess function.

Here's typical timeline:

Form's postprocess function starts

It goes to save X:
  hook_civicrm_pre for X
  X saved to DB
  hook_civicrm_post for X

Then to save Y:
  hook_civicrm_pre for Y
  Y saved to DB
  hook_civicrm_post for Y

and maybe a notification email

and finally before it completes, the hook is fired:
  hook_civicrm_postProcess for the form
You can find me at AGH Strategies.
Need help now?  Civi911 is your go-to for CiviCRM support.

sdragnev

  • I post occasionally
  • **
  • Posts: 40
  • Karma: 0
  • CiviCRM version: 4.6.2
  • CMS version: Drupal 7
  • PHP version: 5.4
Re: hook_civicrm_custom and hook_civicrm_post defeated by form?
April 02, 2013, 07:34:03 am
Thanks for the clarification, postProcess did do it. I also needed hook_civicrm_post to catch the ajaxy inline editing of addresses.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • [Solved] hook_civicrm_custom and hook_civicrm_post defeated by form?

This forum was archived on 2017-11-26.