开发者

Set Drupal Module Weight

开发者 https://www.devze.com 2023-02-04 01:40 出处:网络
When developing a custom module, what is the correct way to set a开发者_开发技巧 module\'s weight?This is the correct way to do it in Drupal 7

When developing a custom module, what is the correct way to set a开发者_开发技巧 module's weight?


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 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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消