Last updated August 25, 2016. Created on January 17, 2007.
Edited by interdruper, enzipher, aklump, neetu morwani. 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(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).

In Drupal 8

Notice there is a function to do this now, rather than a database query. The points about using an install hook are still appropriate in Drupal 8.

module_set_weight('[your_module_name]', [your_preferred_weight]);

In Drupal 7

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:

// 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 heavier, so it moves lower in execution order
db_query("UPDATE {system} SET weight = %d WHERE name = '[your_module_name]'", $weight + 1);

For Drupal 7 this code is as follows:

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

  // 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 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

Specially 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();
}

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Anu Anusha’s picture

You can also use the hook hook module_implements_alter for this.

function your_module_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'form_alter' && isset($implementations['your_module'])) {
    $group = $implementations['your_module'];
    unset($implementations['your_module']);
    $implementations['your_module'] = $group;
  }
}

Here form alter is just an example.If you are using hook form alter then you can represent like this.
It refers to the type of hook you are using.