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 Hooks
Pages: [1]

Author Topic: Custom Hooks  (Read 904 times)

endres.systopia

  • I’m new here
  • *
  • Posts: 9
  • Karma: 0
  • CiviCRM version: 4.4.5
  • CMS version: Drupal
  • MySQL version: 5.5.29
  • PHP version: 5.4.10
Custom Hooks
September 16, 2013, 08:36:53 am
Hi guys (and gals)! Just a brief question: is there a way for extensions to define custom (new) hooks? And if not (I haven't found one): Will there ever be one? How would it work?

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: Custom Hooks
September 16, 2013, 12:22:49 pm

I thinking u can do the following:

1. Create a class called: MyExt_Hook

2. Create a hook function in there called: my_custom_new_hook

3. Implement MyCustomNewHook in a similar manner to one of the functions in CRM_Utils_Hook.php (like function post( ... )

4. Within your code, you can invoke your custom hook via:

MyExt_Hook::my_custom_new_hook

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

endres.systopia

  • I’m new here
  • *
  • Posts: 9
  • Karma: 0
  • CiviCRM version: 4.4.5
  • CMS version: Drupal
  • MySQL version: 5.5.29
  • PHP version: 5.4.10
Re: Custom Hooks
September 19, 2013, 03:35:59 am
Perfect, thanks, Lobo! I didn't know it was that simple. In particular, I didn't expect that you could call the CRM_Utils_Hook::singleton()->invoke(...) from outside the Hook class.

Just so I get this straight: this is meant to be used for communicating between e.g. two extensions? Especially if they don't know (of) each other? For example, extension A would introduce a new hook, and extensions B and C could implement it...

Pretty much like this:

class CRM_Utils_MyHook {
  static function myhook($op, $objectName, $id, &$params) {
    return CRM_Utils_Hook::singleton()->invoke(4, $op, $objectName, $id, $params, $op, 'civicrm_myhook');
  }
}


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: Custom Hooks
September 19, 2013, 07:01:46 am

yep.

i'm pretty sure it will work, but have not tried it myself. So please do try it out and let us know :)

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

endres.systopia

  • I’m new here
  • *
  • Posts: 9
  • Karma: 0
  • CiviCRM version: 4.4.5
  • CMS version: Drupal
  • MySQL version: 5.5.29
  • PHP version: 5.4.10
Re: Custom Hooks
September 19, 2013, 07:11:45 am
It does, I tried!

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: Custom Hooks
September 19, 2013, 07:26:26 am

great :) good to know

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

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

This forum was archived on 2017-11-26.