How do I arrange the order of the function calls from custom modules to different hooks.

I want mymodule_form_alter to be called after secondmodule_form_alter

Comments

gpk’s picture

The order in which hooks are called is determined by 1. the module's weight in the system table and 2. alphabetically I think, if modules have the same weight.

gpk
----
www.alexoria.co.uk

spM-1’s picture

And how to manipulate the weights of the module ? well, you can directly type some sql into the mysql terminal or simply use the Util(http://drupal.org/project/util) modules, Module Weight feature to see and manipulate the module weights on the module installation page :)

Cheers

jaypan’s picture

If you are creating both alter functions (ie. creating both modules), then you can use hook_form_FORM_ID_alter() and hook_form_alter(). hook_form_FORM_ID_alter() is run before hook_form_alter(), so they will be called in the order you want.

Contact me to contract me for D7 -> D10/11 migrations.

Anonymous’s picture

I think the easiest way is to alter the column 'weight' of the 'system' table. The description of the column is quite clear :

The order in which this module’s hooks should be invoked relative to other modules. Equal-weighted modules are ordered by name.

This could be done by simple request :

<?php
db_update('system')
    -> fields(array('weight' => $weight))
    ->condition('name', 'mymodule')
    ->condition('type', 'module')
    ->execute();
?>

Or as said you can use the UTIL module.

hussainweb’s picture

I know this is old and the answer requires Drupal 7, but for the sake of completeness, here it is.

I found this on http://drupal.stackexchange.com/a/9720/13553.

Also worth mentioning, there is a new drupal 7 API called hook_module_implements_alter() which lets you alter the execution order for a given hook WIHOUT altering the module weights table.

Sample code from the API docs showing how easy this is to do:

<?php
function hook_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'rdf_mapping') {
    // Move my_module_rdf_mapping() 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['my_module'];
    unset($implementations['my_module']);
    $implementations['my_module'] = $group;
  }
}
?>