3

I use D7 and need 2 different modules to parse article text on save. For simplicity's sake let's say module_one changes all f's to z's and module_two changes all foos to bars. So basically "F foo fo foo" is to become "Z bar zo bar". To accomplish that I need module_two node_presave event to fire before module1. Otherwise the result will be something like "Z zoo zo zoo". But hook_module_implements_alter() doesn't seem to do the thing. I write it like this:

function module_one_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'node_presave') {
    // Move module_one_node_presave() to the end of the list. module_implements()
    // iterates through $implementations with a foreach loop which PHP iterates
    // in the order that the items were added, so to move an item to the end of
    // the array, we remove it and then add it.
    $group = $implementations['module_one'];
    unset($implementations['module_one']);
    $implementations['module_one'] = $group;
  }
}

This hook executes but I still get module_one's node_presave hook executed before module_two's. So what am I doing wrong?

0

The code you use is similar to the example code shown in the documentation page for hook_module_implements_alter(). It is supposed to be implemented from the module that wants to execute its hook before the other module, but it would work also how you did.

Consider that the list of hooks implemented by modules are cached. If you just edit the code of an existing enabled module, then you would not see any difference in the execution order of that hook implementations.

hook_module_implements_alter() should be implemented when you are interesting to alter the execution order of a single hook implementations, and leave the execution order of the other hooks as they are.
If changing the execution order of all the hooks doesn't cause you any problem, then you should just change the module weight, which is computationally lighter.

Usually a module implements hook_install(), or hook_update_N() using code similar to the following:

function mymodule_install() {
  db_update('system')
    ->fields(array('weight' => $weight))
    ->condition('name', 'mymodule')
    ->execute();
}
1
  • 1
    function mymodule_install() { db_update('system') ->fields(array('weight' => $weight)) ->condition('name', 'mymodule') ->execute(); } – Tim Apr 25 '12 at 5:01
0

Double check the weight in the system table on your d7 database. You can try giving module one a higher weight therefore making it run second.

3
  • 1
    That's what I though too but according to the docs page for hook_module_implements_alter(): " A module may implement this hook in order to reorder the implementing modules, which are otherwise ordered by the module's system weight.". So this should work really – Clive Feb 5 '12 at 21:39
  • @KyleBrowning, as far as I understood hook_module_implements_alter() reorders hooks, not modules. In the system table both modules have weight == 0. Do you think it's better to try modifying database on hook_install()? – Ari Linn Feb 5 '12 at 22:31
  • if module implements alter is not working, I would try the weights. – Kyle Browning Feb 6 '12 at 18:55

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.