Hooks是module定义的特殊名称的funcion。通常用来修改core module的行为或数据。
php方式理解hook
为了完成某项任务定义函数,随着时间的推移,业务的变化,你想要修改这个函数,可是有些人已经使用了这个函数,这个时候该怎么办?
答案是:”永远不要修改既存代码”,hook就是用来解决这个问题的。
drupal的方式理解hook
我们想对drupal core module的功能进行修改,又不修改core module的代码,这个时候就需要用到hook。
当drupal core module中的hook被调用时,drupal会做以下3件事:
- 取得所有可用的module列表
- 询问每一个模块是否实现了对应的hook
- 如果模块回答“是”,drupal就会调用模块中的hook实现
Hooks vs. Event/Observers
Event/Observers say
Hey, just so you know, this happened
Hooks say
Hey, we’re doing this thing, do you want to be part of it?
怎样实现hook
- 去*.api.php中找hook函数定义和对应的示例
- 拷贝1中hook函数到你的module’s .module文件
- 修改hook函数名,替换hook为自定义module名
- 编辑注释为”Implements hook函数名”
- 修改hook函数内容
怎样定义hook
- 给hook函数起个唯一的名字
- 在module’s文件加下创建*.api.php文件,并记述怎样使用这个hook
- 在module’s 代码中调用hook
Types of hooks
- Hooks that answer a question
- Hooks that alter existing data
- Hooks that react to an action
hooks
form系
修改form时,首先想到的是hook_form_alter和hook_form_FORM_ID_alter
也有特殊情况,比如修改register form中password字段label时,就需要用到hook_element_info_alter
hook_form_alter()
|
|
hook_form_FORM_ID_alter()
|
|
hook_element_info_alter()
修改register form中password字段label
views系
hook_views_pre_render()
在views表示前,修改字段的值
参考资料
core hook list
good article discussing how Drupal module/hook system works
Understanding the hook system for Drupal modules
the overview of module hooks
What Are Hooks?
hook_element_info_alter
hook_form_FORM_ID_alter
hook_views_pre_render