When developing a custom module, what is the correct way to set a module's weight?
Set Drupal Module Weight
Asked Answered
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'");
...
}
This looks correct but is the call to drupal_install_schema() required just to set the weight? –
Besot
You could also set the weight by hand... the drupal_install_schema() call is there because devel's install hook needs to install its schema. –
Glorygloryofthesnow
Take 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... –
Paredes
See my answer for the Drupal 7 version. –
Quenby
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();
}
Should be placed in your_module.install file. –
Stob
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'");
...
}
This looks correct but is the call to drupal_install_schema() required just to set the weight? –
Besot
You could also set the weight by hand... the drupal_install_schema() call is there because devel's install hook needs to install its schema. –
Glorygloryofthesnow
Take 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... –
Paredes
See my answer for the Drupal 7 version. –
Quenby
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;
}
© 2022 - 2024 — McMap. All rights reserved.