When developing a custom module, what is the correct way to set a module's weight?
Weight uses the node table's 'sticky' column to store weights as well as sticky information (so that feature is not lost). So then nodes will be sorted first by stickiness, then by weight, then by creation date.
Drupal 8 versionProvides a weight field that can be added to any fieldable entity. The weight field can be used to provide customized sorting.
The term Drupal weight is used to describe the priority or order in which the function is processed, or block/ node is displayed. A heavier (+10) weights will appear lower in lists while a lower weight (-10) will float to the top of lists.
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.
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();
}
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'");
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With