[drupal8]Drupal Hooks Alter的先后顺序

时间:2019-04-07  来源:设计基础  阅读:

Drupal 系统的精髓在于Hooks(钩子)的设计,当系统存在多个模块时,那么模块alter hooks的先后顺序时怎样的呢?带着这个问题,我们找到了正确的答案,就是Drupal module weight 直接影响了hooks在module的执行顺序,默认的情况,每一个module的weight为0,那么order的方法就是按照module的name,这样极易受模块的名称所影响先后顺序。

更新模块Weight
在Drupal 7,模块的weight可以被灵活的修改,通过 hook_module_implements_alter() hook 来实现。

 代码如下

/**
* Implements hook_module_implements_alter().
*/
function custommodule_module_implements_alter(&$implementations, $hook) {
if (in_array($hook, array("form_alter"))) {
// Move our hook implementation to the bottom.
$group = $implementations["custommodule"];
unset($implementations["custommodule"]);
$implementations["custommodule"] = $group;
}
}
?>

另外一个兼容Drupal所有版本的方法时,hook_install 在 “custom_module.install” 文件中。


注意:使用hook_install的时候需要在后台删除模块,再重新启用模块才会生效。

 代码如下

function your_module_name_install() {
db_update("system")
->fields(array("weight" => your_preferred_weight))
->condition("name", "[your_module_name]", "=")
->execute();
}
?>

你也通过一个代码片段来直接更新数据库

 代码如下

db_query("UPDATE {system} SET weight = [your_preferred_weight] WHERE type = "module" AND name = "[your_module_name]"");
?>

也可以通过第三方模块来修改,可以使用 Utility 模块和Modules weight模块。

Drupal 系统模块列表(包含name和weight):

+----------------------------+--------+--------+--------+
| name                       | type   | status | weight |
+----------------------------+--------+--------+--------+
| strongarm                  | module |      1 |  -1000 |
| block                      | module |      1 |     -5 |
| webform                    | module |      1 |     -1 |
| backup_migrate             | module |      1 |      0 |
| colorbox                   | module |      1 |      0 |
| contextual                 | module |      1 |      0 |
| crumbs                     | module |      1 |      0 |
| ctools                     | module |      1 |      0 |
| date                       | module |      1 |      0 |
| date_api                   | module |      1 |      0 |
| date_views                 | module |      1 |      0 |
| entity                     | module |      1 |      0 |
| faq                        | module |      1 |      0 |
| field                      | module |      1 |      0 |
| field_ui                   | module |      1 |      0 |
| file                       | module |      1 |      0 |
| filter                     | module |      1 |      0 |
| image                      | module |      1 |      0 |
| libraries                  | module |      1 |      0 |
| link                       | module |      1 |      0 |
| list                       | module |      1 |      0 |
| locale                     | module |      1 |      0 |
| masquerade                 | module |      1 |      0 |
| menu                       | module |      1 |      0 |
| menu_block                 | module |      1 |      0 |
| node                       | module |      1 |      0 |
| options                    | module |      1 |      0 |
| path                       | module |      1 |      0 |
| print                      | module |      1 |      0 |
| search                     | module |      1 |      0 |
| shortcut                   | module |      1 |      0 |
| taxonomy                   | module |      1 |      0 |
| taxonomy_manager           | module |      1 |      0 |
| text                       | module |      1 |      0 |
| token                      | module |      1 |      0 |
| translation                | module |      1 |      0 |
| transliteration            | module |      1 |      0 |
| update                     | module |      1 |      0 |
| user                       | module |      1 |      0 |
| views_ui                   | module |      1 |      0 |
| wysiwyg                    | module |      1 |      0 |
| dblog                      | module |      1 |      0 |
| domain                     | module |      1 |      0 |
| mollom                     | module |      1 |      0 |
| overlay                    | module |      1 |      0 |
| system                     | module |      1 |      0 |
| field_group                | module |      1 |      1 |
| pathauto                   | module |      1 |      1 |
| i18n                       | module |      1 |     10 |
| i18n_string                | module |      1 |     10 |
| views                      | module |      1 |     10 |
| rules                      | module |      1 |     20 |
| admin_menu                 | module |      1 |    100 |
+----------------------------+--------+--------+--------+

你可以通过这个页面找到Drupal中所有核心的Hooks,它可能会对你非常有用。

[drupal8]Drupal Hooks Alter的先后顺序

http://m.bbyears.com/wangyezhizuo/49263.html

推荐访问:drupal教程 drupal_add_css drupal表单
相关阅读 猜你喜欢
本类排行 本类最新