Answers.Body: <p>My original answer in 2016 is now out of date.  In Civi 4.7.19+, you can control the order of hooks with the <a href="https://docs.civicrm.org/dev/en/latest/hooks/setup/symfony/" rel="nofollow noreferrer">Symfony EventDispatcher</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-30T15:21:25.063Z
Answers.LastActivityDate: 2018-11-30T15:21:25.063Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.Body: <p>CRM/Utils/Hook.php indirectly calls CRM/Extension/Mapper.php::getActiveModuleFiles which does</p>
  
  <p>SELECT full_name, file
          FROM civicrm_extension
          WHERE is_active = 1
          AND type = "module"</p>
  
  <p>So there is no well-defined order. You could of course hack that line to add an ORDER BY.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-30T22:28:36.557Z
Answers.LastActivityDate: 2016-09-30T22:28:36.557Z
Answers.UserDisplayName: Demerit
Answers.Body: <p>I got some great answers on Mattermost.</p>
  
  <p>Frank Gomez of Ginkgo Street Labs says:
  We've developed an approach (which you may find heavy handed) to dealing with order of operation problems when it comes to hook_civicrm_post. In our case the problem was not the order in which extensions were firing it but rather that dependent entities might not exist yet. Basically the approach is to defer execution of the code by creating a queue item to deal with it on the next page load, when the dependent entities are expected to have been created.</p>
  
  <p>Here's the repo: <a href="https://github.com/ginkgostreet/com.ginkgostreet.listener" rel="nofollow">https://github.com/ginkgostreet/com.ginkgostreet.listener</a>.</p>
  
  <p>Allen Shaw of Joinery points to Tim Otten's post <a href="https://forum.civicrm.org/index.php?topic=36667.0" rel="nofollow">here</a>, the most notable part is:
  [You can't] - 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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-06T21:17:51.330Z
Answers.LastActivityDate: 2016-10-06T21:17:51.330Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.Body: <p>I am not sure if you can, perhaps by making sure the sequence is as you would want in civicrm_system?</p>
  
  <p>Can you explain a little about what you want to do so I can perhaps come up with another alternative (you never know....)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-30T18:25:47.717Z
Answers.LastActivityDate: 2016-09-30T18:25:47.717Z
Answers.UserDisplayName: ErikH - CiviCooP
Body: <p>If I have two extensions that both use, say, <code>hook_civicrm_post</code>, how can I control the order in which the two extensions fire?</p>
  
Tags: <developer><hooks>
Title: How do I control the order in which hooks fire?
ThreadText: # How do I control the order in which hooks fire?
  
  (Asked by: username_0 on 2016-09-30)
  
  If I have two extensions that both use, say, `hook_civicrm_post`, how can I control the order in which the two extensions fire?
  
  **Tags:** developer, hooks
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_0 on 2018-11-30. Score: 0 | Accepted answer
  
  My original answer in 2016 is now out of date. In Civi 4.7.19+, you can control the order of hooks with the [Symfony EventDispatcher](https://docs.civicrm.org/dev/en/latest/hooks/setup/symfony/).
  
  
  ---
  
  ### Answer by username_1 on 2016-09-30. Score: 2
  
  CRM/Utils/Hook.php indirectly calls CRM/Extension/Mapper.php::getActiveModuleFiles which does
  
  SELECT full\_name, file
  FROM civicrm\_extension
  WHERE is\_active = 1
  AND type = "module"
  
  So there is no well-defined order. You could of course hack that line to add an ORDER BY.
  
  
  ---
  
  ### Answer by username_0 on 2016-10-06. Score: 1
  
  I got some great answers on Mattermost.
  
  Frank Gomez of Ginkgo Street Labs says:
  We've developed an approach (which you may find heavy handed) to dealing with order of operation problems when it comes to hook\_civicrm\_post. In our case the problem was not the order in which extensions were firing it but rather that dependent entities might not exist yet. Basically the approach is to defer execution of the code by creating a queue item to deal with it on the next page load, when the dependent entities are expected to have been created.
  
  Here's the repo: <https://github.com/ginkgostreet/com.ginkgostreet.listener>.
  
  Allen Shaw of Joinery points to Tim Otten's post [here](https://forum.civicrm.org/index.php?topic=36667.0), the most notable part is:
  [You can't] - 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.
  
  
  ---
  
  ### Answer by username_2 on 2016-09-30. Score: 0
  
  I am not sure if you can, perhaps by making sure the sequence is as you would want in civicrm\_system?
  
  Can you explain a little about what you want to do so I can perhaps come up with another alternative (you never know....)
  
  
  ---
  
  