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 (Moderator: Donald Lobo) »
  • Order of extensions hooks
Pages: [1]

Author Topic: Order of extensions hooks  (Read 468 times)

mallezie

  • I post occasionally
  • **
  • Posts: 33
  • Karma: 0
  • CiviCRM version: 4.5
  • CMS version: Drupal
  • PHP version: 5.4
Order of extensions hooks
August 04, 2015, 01:48:18 am
Hello there,

I was wondering if CiviCRM extensions have a concept of weight's to take control of the order of hooks executed by them.

For example, i have two custom extension, which both implement hook_civicrm_post()
The first implementation fills in a custom field based on an external API call.
The second implementation does some calculations with the filled in field, so i wan't to make sure the hooks are executed in a defined order.
I know in drupal, a module has a 'weight ' to achieve this, but i'm not sure CiviCRM extensions have an analogue concept.

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Order of extensions hooks
August 04, 2015, 03:11:28 am
a) It does not - in fact, I suspect the order is non-deterministic, with a slight bias towards installation-order (ie the extension which was installed first would tend to run first). I came to this belief from tracing CRM_Utils_Hook::commonInvoke() => commonBuildModuleList() => requireCiviModules() ...etc... finally arriving at CRM_Extension_Mapper::getActiveModuleFiles(), which queries the DB and does not have an ORDER BY.

b) There's an extra element worth noting: when Civi fires an event, it actually fires ~2 times (once with the CMS's hook/event system, and once with the extensions' hook/event system). Weights in the two systems would not be able to mingle. Chalk that up to "edge-cases from having two systems".

c) I've posted a rant arguing to consolidate the event subsystems - https://forum.civicrm.org/index.php?topic=36086.0 . Just to pitch this a little more... if we consolidated on Symfony's EventDispatcher, then it would possible to define weights for each listener.

For a little follow-up/recap on that thread: we've been making baby steps with EventDispatcher in core. For example, in Civi\Core\Container::createDispatcher, we register some core listeners for hook_civicrm_post.

When PR #6297 gets merged (esp. https://github.com/totten/civicrm-core/commit/8ba6fb8c9f2052656cae06547eae91cdc932ec6c ), I believe that any type of package will be able to work with EventDispatcher (eg Civi extensions/Drupal modules/Joomla extensions/WordPress plugins). The basic technique is to build on hook_civicrm_container like so:

Code: [Select]
function mymodule_civicrm_container(ContainerBuilder $c) {
  $c->findDefinition('dispatcher')->addMethodCall('addListener', array(
    'hook_civicrm_post',
    'my_custom_callback_func',
    1234 /* the desired weight; omit for default */
   ));
}

That nominally does what you're asking about, although it doesn't give full flexibility (because we can't have weights that cut across fundamentally different event systems), and it's only one step on a longer path toward consolidated events.

mallezie

  • I post occasionally
  • **
  • Posts: 33
  • Karma: 0
  • CiviCRM version: 4.5
  • CMS version: Drupal
  • PHP version: 5.4
Re: Order of extensions hooks
August 04, 2015, 05:28:52 am
Thanks for the great explanation.

For now i'll work around it. The event dispatcher from symfony could indeed be a solution, i now Drupal 8 uses it partiallay, although as a full replacement for Drupal hooks that will probably only happen in D9.
They had some issues by the event ordering see https://www.drupal.org/node/2344645 but AFAICT that's more since drupal even allows to override / alter the ordering.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Order of extensions hooks

This forum was archived on 2017-11-26.