Set Drupal Module Weight
10,417
Solution 1
The standard way is to do it in a query in the install hook.
From the devel module:
/**
* Implementation of hook_install()
*/
function devel_install() {
drupal_install_schema('devel');
// New module weights in core: put devel as the very last in the chain.
db_query("UPDATE {system} SET weight = 88 WHERE name = 'devel'");
...
}
Solution 2
This is the correct way to do it in Drupal 7
/**
* Implements hook_enable()
*/
function YOUR_MODULE_enable() {
db_update('system')
->fields(array('weight' => 1))
->condition('type', 'module')
->condition('name', 'YOUR_MODULE')
->execute();
}
Solution 3
if for some reason you have to stick it in an update hook, you will want to properly return the result from update_sql, lest you get nasty-looking innocuous errors.
function mymodule_update_6000(&$sandbox) {
$res[] = update_sql("UPDATE {system} SET weight = 1 WHERE name = 'mymodule'");
return $res;
}
Related videos on Youtube
Author by
markdorison
Updated on August 21, 2020Comments
-
markdorison over 3 years
When developing a custom module, what is the correct way to set a module's weight?
-
markdorison over 13 yearsThis looks correct but is the call to drupal_install_schema() required just to set the weight?
-
David Eads over 13 yearsYou could also set the weight by hand... the drupal_install_schema() call is there because devel's install hook needs to install its schema.
-
Shushu over 13 yearsTake into consideration that setting the weight is not always all you need to do. In some cases I encountered, setting the "bootstrap" was required as well, and modules with lower weight but with "bootstrap" is loaded before "standard" modules - take that into account...
-
jackocnr over 11 yearsSee my answer for the Drupal 7 version.
-
duru about 9 yearsShould be placed in your_module.install file.