Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Drupal Module Weight

When developing a custom module, what is the correct way to set a module's weight?

like image 454
markdorison Avatar asked Jan 12 '11 16:01

markdorison


People also ask

What does weight mean in Drupal?

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.

What is weight Drupal 8?

Drupal 8 versionProvides a weight field that can be added to any fieldable entity. The weight field can be used to provide customized sorting.

What is true about Drupal weight?

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.

What property affects a module execution order by Drupal core?

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.


2 Answers

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();
}
like image 179
jackocnr Avatar answered Sep 18 '22 09:09

jackocnr


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'");

  ...
}
like image 40
googletorp Avatar answered Sep 21 '22 09:09

googletorp