Can I change the order in which hook implementation by different modules are called?

drupal.stackexchange https://drupal.stackexchange.com/questions/404

  •  16-10-2019
  •  | 
  •  

Question

In D6, I use a contrib module, but I would like to override a small bit of its behavior. I have a custom module for that, but my implementation of the relevant hook is called before the implementation in the contrib module, and so the contrib module's version "wins".

Is there any way to change it, and force my own hook to be called last?

Here are the specific details:

I would like to remove the default "Anonymous" user name for unregistered users in the comment form. I created a small custom module, tweak_comment_form, that implements form_alter hook and removes the default value from the name field (see the answers to my previous question about that).

However I also use a contrib module called RealName, which also implements form_alter. RealName does what I want for registered users (replaces user name with "real name"), but re-assigns default value to the name field.

Was it helpful?

Solution

Yes, very simply, you need to set the weight of your module to be heavier. There's a few ways you can do this, but if your module is a contrib module being release back to Drupal.org then the best way is to set it in hook_install() with code like:

db_query("UPDATE {system} SET weight = 100 WHERE name = 'MYMODULE'");

Alternatively you can use the Utility module to set module weights from the module page, however this is only for your personal installation.

OTHER TIPS

Just as additional helpful information in drupal7 you have hook_module_implements_alter.

There you can change hooks of other modules, too, for example move you module to the end/the start.

function my_module_module_implements_alter(&$implementations, $hook) {
  if ($hook != 'the_hook_to_change') {
    return;
  }
  $module = 'my_module';
  $group = array($module => $implementations[$module]);
  unset($implementations[$module]);

  $implementations = $group + $implementations; // If you want your implementation to run first.
  //$implementations = $group + $implementations; If you want your implementation to run last.
}

Small Modification based on https://api.drupal.org/comment/48623#comment-48623

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top