Set Drupal Module Weight
Asked Answered
B

3

13

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

Besot answered 12/1, 2011 at 16:37 Comment(0)
W
20

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

  ...
}
Wabble answered 12/1, 2011 at 16:47 Comment(4)
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
Q
25

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();
}
Quenby answered 20/8, 2012 at 17:42 Comment(1)
Should be placed in your_module.install file.Stob
W
20

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

  ...
}
Wabble answered 12/1, 2011 at 16:47 Comment(4)
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
R
4

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;
}
Rather answered 13/4, 2011 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.