Home > drupal, drupal 7, execution, hooks > Drupal 7 – Hook Execution Order

Drupal 7 – Hook Execution Order

December 6, 2010 Leave a comment Go to comments

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
 // 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!

  1. January 15, 2011 at 5:02 pm

    Thanks for posting this, I didn’t realize this hook had been added… very helpful!

  2. Michael
    March 6, 2012 at 12:56 am

    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!

    • Kieran Mathieson
      October 5, 2012 at 6:46 pm

      I have the same issue. Did you figure it out?

  3. lucianocossich
    September 20, 2012 at 4:02 am

    whoah! that did it for me! thank you, thank you, thank you. 🙂

  4. July 1, 2013 at 1:14 pm

    Лучшие статусы для контакта которы могут показать ваше настроение.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: