Drupal 7 – Hook Execution Order
Although not specified explicitly anywhere, browsing through the code in the function “module_implements” gave me the hint that changing the order in which hooks are called is possible.
Here’s that snippet
// Allow modules to change the weight of specific implementations but avoid
// an infinite loop.
if ($hook != 'module_implements_alter') {
drupal_alter('module_implements', $implementations[$hook], $hook);
}
All that needs to be done is to implement the hook hook_module_implements_alter specified here. Effectively by re-ordering the list of modules, you also change the order in which the hooks are called.
Here’s how I did it,
function mymodule_module_implements_alter(&$module_list, $context){
if($context === "node_insert"){
$temp = $module_list['mymodule'];
// Removing the mymodule key/value
unset($module_list['mymodule']);
// Adding the mymodule key value as the last member in the list
$module_list['mymodule'] = $temp;
}
}
One point to remember is to declare the “module_list” variable as a reference variable with the “&”, or you’ll be modifying a local variable ![]()
Thats it. Done!
Categories: drupal, drupal 7, execution, hooks
change order, hook execution order

Thanks for posting this, I didn’t realize this hook had been added… very helpful!
Hello, and thank you for posting this article. I tried to implement it within a new custom module I’m working on (called teachandlearn). I started off by simply outputting the parameters:
function teachandlearn_module_implements_alter( &$module_list, $context ) {
print( ‘$context = ‘ . $context . ‘, $module_list = ‘ ); print_r( $module_list );
}
I expected the output to include a list of all the modules called within Drupal, but instead all it output was:
$context = field_display_alter, $module_list = Array
(
[node] =>
[token] =>
)
$context = query_alter, $module_list = Array
(
[node] =>
)
There isn’t even an array element for the current module, $module_list[ 'teachandlearn' ]. Any idea what I’m doing wrong? Thanks!