7

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.

7

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.

  • 1
    Thanks, that's very helpful. I guess if I just want a quick fix and don't want to install the Utility module, I can simply update the database... – daphshez Mar 6 '11 at 17:35
  • It's usual, for a module that needs to run after another one, to change its own weight in the "system" table. None of the modules of which I know the code depends from the Utility module to alter its own weight. – kiamlaluno Mar 6 '11 at 22:43
  • @kiamlaluno I wasn't suggesting Utility as a dependency, I was suggesting it as a Utility for OP to change the weight on his own system. If he was to be releasing a contrib module, then no, it would definitely not be a solution. – Decipher Mar 6 '11 at 22:45
  • 1
    I took the question was being asked from the developer point of view, and I took your suggestion was for the developer. Now I understood you meant that to change the weight of a module installed on your own site (or any sites you administer) is not necessary to create a custom module just for changing the weight of another module. – kiamlaluno Mar 7 '11 at 11:34
4

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.

  • I think this should be the accepted answer :) – Gokul N K Feb 4 '14 at 9:34
  • Can you give us a short example? – leymannx Aug 22 '14 at 11:11
1
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.