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) »
  • _custom versus _pre and _post
Pages: [1]

Author Topic: _custom versus _pre and _post  (Read 922 times)

John.K

  • I post occasionally
  • **
  • Posts: 75
  • Karma: 5
  • CiviCRM version: 4.x
  • CMS version: Drupal 7
  • MySQL version: 5.x
  • PHP version: 5
_custom versus _pre and _post
April 15, 2015, 06:43:01 am
CiviCRM offers the _pre and _post database hooks to alter data between the database, but they are not called for custom fields.

The _custom database hook is called after a custom field is written/deleted on the database.

Why is there a different hook for custom fields, rather than just implementing the _pre and _post hooks in place of _custom? (or as well as)

Here's the difficulty / inconsistency:

#1 If I use the JS inline editor for a contact and update a group of custom fields: _pre and _post are not called, but _custom is called.

#2 If I put custom fields on a profile, and update it: _pre IS called (for the 'Profile') entity, and contains the value of the custom field. Then _custom is also called. Then _post.

#3 API calls presumably (I haven't tested this) always trigger _pre and _post, and therefore also _custom, because they always target a Civi entity.

This effectively means that you can't rely on _post to pick up all database updates; and you can't really use _post and _custom because they might both be called.

Proposed resolution:

Wouldn't it make more sense to deprecate _custom, and just use _pre and _post? Custom fields are always attached to an entity, so we should be able to use the existing functions. The only trouble is making sure _pre and _post aren't called twice, like in example #2 above.

--

This is all very early days in the thought process, but I'd appreciate all your feedback and thoughts on this =]
« Last Edit: April 15, 2015, 08:47:02 am by John.K »

John.K

  • I post occasionally
  • **
  • Posts: 75
  • Karma: 5
  • CiviCRM version: 4.x
  • CMS version: Drupal 7
  • MySQL version: 5.x
  • PHP version: 5
Re: _custom versus _pre and _post
May 20, 2015, 07:42:08 am
Cross-linking to a recent issue on the topic in JIRA: https://issues.civicrm.org/jira/browse/CRM-15261

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: _custom versus _pre and _post
May 21, 2015, 07:25:26 am
I think yes those inline-edit forms for custom data really ought to be calling _pre and _post hooks.

I'm unsure about deprecating the _custom hook, not sure exactly the history behind it and who might be using it. Would be good to get some more feedback on that.
Try asking your question on the new CiviCRM help site.

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: _custom versus _pre and _post
May 28, 2015, 02:17:54 pm
Related: https://issues.civicrm.org/jira/browse/CRM-16583

My 2 cents: getting rid of hook_civicrm_custom would be better for consistency and maintainability than introducing a new custom_pre hook.
Try asking your question on the new CiviCRM help site.

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: _custom versus _pre and _post
May 29, 2015, 12:43:04 am
Agree with the principle: should be pre and post. We do have some extensions that use the _custom hook as it is what was available.
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

rocxa

  • I post occasionally
  • **
  • Posts: 40
  • Karma: 4
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.71
  • PHP version: 5.3.3
Re: _custom versus _pre and _post
May 29, 2015, 01:51:46 am
My vote would also be for removing hook_civicrm_custom and just using _pre and _post consistently.  It will probably break many extensions and custom modifications people have done though which is why I thought just adding custom_pre was simpler as a short term (non breaking) fix.

On a side note, the hook_civicrm_custom has a params array and not an objectRef which have differently layouts of the data despite holding the same sort of information.

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: _custom versus _pre and _post
May 29, 2015, 06:35:05 am
Sounds like we have general agreement.
@rocxa I think the original suggestion offers a non-breaking fix for both short and long term. It was to ensure _pre and _post are always called for custom data, but don't remove hook_civicrm_custom. We can deprecated it and give people time to migrate their code before we remove it down the road.
Try asking your question on the new CiviCRM help site.

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: _custom versus _pre and _post
May 29, 2015, 08:11:39 am
John K do you want to create a PR for this?
Try asking your question on the new CiviCRM help site.

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: _custom versus _pre and _post
May 30, 2015, 10:19:27 am
Or @rocxa I'm sure if you wanted to create the PR yourself John wouldn't mind :)
Try asking your question on the new CiviCRM help site.

John.K

  • I post occasionally
  • **
  • Posts: 75
  • Karma: 5
  • CiviCRM version: 4.x
  • CMS version: Drupal 7
  • MySQL version: 5.x
  • PHP version: 5
Re: _custom versus _pre and _post
June 04, 2015, 02:06:06 am
Great, I've updated the issue here with the agreed proposed resolution: https://issues.civicrm.org/jira/browse/CRM-15261


Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • _custom versus _pre and _post

This forum was archived on 2017-11-26.