你能在 Drupal 中创建你自己的 Hook 吗?
是否可以在 Drupal 模块中创建您自己的钩子供其他 Drupal 模块使用?如果没有,Drupal 中是否有第三方开发人员提供钩子的机制?如果到目前为止一切都没有,那么在核心中实现的钩子列表在哪里?
Is it possible to create your own hook in a Drupal module for other Drupal modules to consume? If not, is there a mechanism in Drupal for third party developers to provide hooks? If everything's been a no so far, where in the core are the list of hooks implemented?
据我所知,Drupal 模块在一个名为 的系统上工作钩子.当您创建新模块时,您将创建实现挂钩的函数.例如,有一个 hook_delete
钩子.如果你在你的模块中实现了一个函数
As I understand things, Drupal modules work on a event like system called hooks. When you create a new module, you create functions that implement a hook. For example, there's a hook_delete
hook. If you implement a function in your module
function mymodule_delete($node)
{
}
每当删除节点时都会调用此函数.
this function will be called whenever a node is deleted.
我想知道的是,作为第三方模块开发人员,有没有办法创建我自己的钩子.比如说,像 hook_alanskickbutthook
这样的东西,以便其他模块开发人员可以订阅这个钩子.
What I want to know is, is there a way or me, as a third party module developer, to create my own hooks. Say, something like hook_alanskickbutthook
so that other module developers could subscribe to this hook.
如果可以,你会怎么做?我查看了官方文档,并没有找到太多内容,当我开始查看 Drupal 源代码时,我仍然有点头晕(我了解递归,但没有花足够的时间考虑递归问题).欢迎提供完整的解决方案,但我很高兴能指出正确的方向.
If this is possible, how do you do it? I've looked around the official docs and haven't found much there, and I still get a little dizzy when I start poking around the Drupal source code (I understand recursion, but don't spend enough time thinking about recursive problems). Full solutions are welcome, but I'm happy to just be pointed in the right direction.
推荐答案
Module_invoke_all() 是您创建自己的钩子的门票:
Module_invoke_all() is your ticket to creating your own hooks:
查看 API:
http://api.drupal.org/api/drupal/includes--module.inc/function/module_invoke_all
然后看看这篇很棒的文章:
and then look at this great writeup:
http://web.archive.org/web/20101227170201/http://himerus.com/blog/himerus/creating-hooks-your-drupal-modules
(位于 http://himerus.com/blog/himerus/creating-hooks-your-drupal-modules 但现在已经没有了)
(edit: was at http://himerus.com/blog/himerus/creating-hooks-your-drupal-modules but this is now gone)
创建钩子后,可以使用以下命令在另一个模块中调用它:
Once you've made your hook, it can be called in another module using:
/**
* Implementation of hook_myhookname()
*/
function THISMODULENAME_myhookname(args){
//do stuff
}
相关文章