How to update a module's weight

Last updated on
21 September 2023

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

In Drupal, the order in which a module's hooks get called is dependent on the weight of your module.

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:

Drupal 8+

See the Drupal 8 wiki page on hooks

Drupal 7

you can look in the system table or use the module_list() function:

$modules = implode(PHP_EOL, module_list());
print $modules;

SQL to update weight

If you have direct access to your database you can change the weight with a query:

UPDATE system SET weight=999 WHERE name = 'mycustommodule'

Note that it's not recommended to do this on production websites.

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).

db_query("UPDATE {system} SET weight = [your_preferred_weight] WHERE type = 'module' AND name = '[your_module_name]'");
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 heavier than another module's (so that it executes after the other module), you can use the following code for Drupal 7:

// Find out the weight of the other module
$weight = db_query("SELECT weight FROM {system} WHERE name = '[the_other_module_name]'")->fetchObject()->weight;

// Set our module to a weight 1 heavier, so it moves lower in execution order
db_query("UPDATE {system} SET weight = :weight WHERE name = '[your_module_name]'", array(':weight' => $weight + 1));

Or this code:

  // Get the weight of the module we want to compare against
  $result = db_select('system', 's')
              ->fields('s', array('weight'))
              ->condition('name', '[the_other_module_name]', '=')
              ->execute();
  $weight = !empty($result) ? $result->fetchField() : 0;

  // Set our module to a weight 1 heavier, so ours moves lower in execution order
  db_update('system')
    ->fields(array('weight' => $weight + 1))
    ->condition('name', '[your_module_name]', '=')
    ->execute();

Changing module weight via UI

The contributed modules modules_weight and Utility offer a UI to change the weights of modules on a site.

Example module weights

Find below an excerpt from the system database table that shows the weight of the 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.

Changing weight using a hook_update_N implementation

Especially useful when you want this inside a hook_update_N() implementation, for example:

/**
 * Set weight of the My Funky Module module to 13.
 */
function my_funky_module_update_7100() {
    db_update('system')
    ->fields(array('weight' => 13))
    ->condition('name', 'my_funky_module', '=')
    ->execute();
}

Help improve this page

Page status: No known problems

You can: