Last updated March 12, 2013. Created by rmiddle on January 17, 2007.
Edited by dumbass, drupalshrek, mitchell, batigolix. Log in to edit this page.
In Drupal, the order in which a module's hooks get called is dependent on the weight of your module in the system table. You can set a low weight (including negative numbers) to get your module to execute before others. Or, you can set a high weight to execute after other modules.
If no weight is defined modules get a default weight of 0.
To find out the order in which the modules are executed you can look in the system table or use the the module_list() function:
$modules = implode(', ', module_list());
print $modules;Variable weight per hook
In Drupal 7, modules can vary the weights of their hooks to be more flexible. For details, see hook_module_implements_alter() in the API documentation.
Code to update weight
You will want to modify and then place this code into your module's modulename.install file in an implementation of hook_install (i.e. within a [your_module_name]_install function).
<?php
db_query("UPDATE {system} SET weight = [your_preferred_weight] WHERE type = 'module' AND name = '[your_module_name]'");
?>In Drupal 7
<?php
function your_module_name_install(){
db_update('system')
->fields(array('weight' => your_preferred_weight))
->condition('name', '[your_module_name]', '=')
->execute();
}
?>If you want your module's weight to be 1 higher than another module's, you can use the following code:
<?php
// Find out the weight of the other module
$weight = db_result(db_query("SELECT weight FROM {system} WHERE name = '[the_other_module_name]'"));
// Set our module to a weight 1 higher
db_query("UPDATE {system} SET weight = %d WHERE name = '[your_module_name]'", $weight + 1);
?>For Drupal 7 this code is as follows:
<?php
$weight = db_select('system', 's')
->fields('s', array('weight'))
->condition('name', '[the_other_module_name]', '=')
->execute()
->fetchField();
db_update('system')
->fields(array('weight' => $weight +1))
->condition('name', '[your_module_name]', '=')
->execute();
?>Utility module
You can also use the Utility module for an admin interface to change weights of modules installed on your site.
Example module weights
Find below an excerpt from the system database table that shows the weight of common core and contributed modules. Core modules all use a weight of zero.
+----------------------------+--------+--------+--------+
| name | type | status | weight |
+----------------------------+--------+--------+--------+
| strongarm | module | 1 | -1000 |
| block | module | 1 | -5 |
| webform | module | 1 | -1 |
| acquia_search | module | 1 | 0 |
| backup_migrate | module | 1 | 0 |
| colorbox | module | 1 | 0 |
| contextual | module | 1 | 0 |
| crumbs | module | 1 | 0 |
| ctools | module | 1 | 0 |
| date | module | 1 | 0 |
| date_api | module | 1 | 0 |
| date_views | module | 1 | 0 |
| entity | module | 1 | 0 |
| faq | module | 1 | 0 |
| field | module | 1 | 0 |
| field_ui | module | 1 | 0 |
| file | module | 1 | 0 |
| filter | module | 1 | 0 |
| geocoder | module | 1 | 0 |
| geofield | module | 1 | 0 |
| geophp | module | 1 | 0 |
| image | module | 1 | 0 |
| libraries | module | 1 | 0 |
| link | module | 1 | 0 |
| list | module | 1 | 0 |
| locale | module | 1 | 0 |
| masquerade | module | 1 | 0 |
| menu | module | 1 | 0 |
| menu_block | module | 1 | 0 |
| node | module | 1 | 0 |
| options | module | 1 | 0 |
| path | module | 1 | 0 |
| plupload | module | 1 | 0 |
| print | module | 1 | 0 |
| search | module | 1 | 0 |
| shortcut | module | 1 | 0 |
| taxonomy | module | 1 | 0 |
| taxonomy_manager | module | 1 | 0 |
| text | module | 1 | 0 |
| token | module | 1 | 0 |
| translation | module | 1 | 0 |
| transliteration | module | 1 | 0 |
| update | module | 1 | 0 |
| user | module | 1 | 0 |
| views_ui | module | 1 | 0 |
| wysiwyg | module | 1 | 0 |
| dblog | module | 1 | 0 |
| domain | module | 1 | 0 |
| mollom | module | 1 | 0 |
| overlay | module | 1 | 0 |
| system | module | 1 | 0 |
| acquia_agent | module | 1 | 0 |
| ckeditor_link | module | 1 | 1 |
| ds | module | 1 | 1 |
| field_group | module | 1 | 1 |
| pathauto | module | 1 | 1 |
| i18n | module | 1 | 10 |
| i18n_string | module | 1 | 10 |
| views | module | 1 | 10 |
| rules | module | 1 | 20 |
| admin_menu | module | 1 | 100 |
+----------------------------+--------+--------+--------+Updating the weight of existing modules
A common situation is that you may need to change the weight of an existing module you have installed on your site. You should firstly disable your module(admin>modules or drush dis my_module_name) then uninstall your module (admin>modules>uninstall or drush pm-uninstall my_module_name). Once you have added your .install file you can then enable your module(admin>modules or drush en my_module_name) which will fire your hook_install function and update the weight in the system table.