Drupal是怎么知道,有哪些触发器是可以显示在触发器用户界面的?按照典型的方式,它能够让模块通过钩子声明该模块所实现的钩子。例如,这里是来自comment.module的hook_hook_info()实现。定义触发器描述的地方就是hook_hook_info()的实现:
/**
* Implementation of hook_hook_info().
*/
function comment_hook_info() {
return array(
'comment' => array(
'comment' => array(
'insert' => array(
'runs when' => t('After saving a new comment'),
),
'update' => array(
'runs when' => t('After saving an updated comment'),
),
'delete' => array(
'runs when' => t('After deleting a comment')
),
'view' => array(
'runs when' => t('When a comment is being viewed by an
authenticated user')
),
),
),
);
}
如果我们安装了一个名为monitoring.module的模块,它向Drupal引入了一个新的名为monitoring(监控)的钩子,它可以这样描述该钩子下面的两个操作(overheating(过热)和freezing(过冷)):
/**
* Implementation of hook_hook_info().
*/
function monitoring_hook_info() {
return array(
'monitoring' => array(
'monitoring' => array(
'overheating' => array(
'runs when' => t('When hardware is about to melt down'),
),
'freezing' => array(
'runs when' => t('When hardware is about to freeze up'),
),
),
),
);
}
在启用了监控模块以后,Drupal就能够看到新的hook_hook_info()实现,并修改触发器页面,为新钩子包含一个单独的标签,如图3-6所示。当然,模块本身仍然需要使用module_invoke()或者module_invoke_all()来触发钩子,以及负责触发相应的动作。在这个例子中,该模块需要调用module_invoke_all('monitoring', 'overheating')。它接着需要实现hook_monitoring($op),并使用actions_do()来触发动作。对于一个简单的具体实现,可参看modules/trigger/trigger.module中的trigger_cron()。
图 3-6.新定义的触发器以一个标签的形式显示在了触发器用户界面