你能在 Drupal 中创建自己的 Hook 吗?
是否可以在 Drupal 模块中创建您自己的挂钩以供其他 Drupal 模块使用?如果没有,Drupal 中有没有一种机制可以让第三方开发者提供钩子?如果到目前为止一切都是“否”,那么钩子列表在核心的哪里实现的?
据我了解,Drupal 模块在一个名为 hooks。当您创建新模块时,您将创建实现挂钩的函数。例如,有一个 hook_delete
挂钩。如果您在模块中实现一个函数,则
function mymodule_delete($node)
{
}
每当删除节点时都会调用该函数。
我想知道的是,作为第三方模块开发人员,我是否有办法创建我自己的挂钩。例如,类似 hook_alanskickbutthook
的内容,以便其他模块开发人员可以订阅此挂钩。
如果这是可能的,你会怎么做?我查遍了官方文档,并没有找到太多东西,当我开始研究 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?
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.
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Module_invoke_all() 是您创建自己的钩子的门票:
请参阅 API:
http:// /api.drupal.org/api/drupal/includes--module.inc/function/module_invoke_all
然后看看这篇精彩的文章:
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 但这现在已经消失了)
一旦你创建了你的钩子,就可以使用以下方法在另一个模块中调用它:
Module_invoke_all() is your ticket to creating your own hooks:
see the 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
(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:
例如,假设您想创建 hook_my_custom_goodness() 供其他人使用。然后只需将这样的代码放在模块中您希望挂钩发生的位置:
现在,如果有人想使用您的挂钩,那么他们可以在自己的模块中这样做,如下所示:
这里有更完整的解释:
http://tylerfrankenstein.com/code/drupal-create-custom-hook-for-其他模块
For example, say you wanted to create hook_my_custom_goodness() for others to use. Then just place code like this in your module at the point where you want the hook to happen:
Now, if anybody wanted to use your hook, then they could do so in their own module like this:
There is a more complete explanation here:
http://tylerfrankenstein.com/code/drupal-create-custom-hook-for-other-modules
您需要实现两个钩子 1. hook_token_info() & 2. 模块文件中的 hook_tokens() 下面我给出了创建自定义令牌“query-param-all”的代码,并在视图 - Textarea 字段中使用该令牌......
You need to implement two hooks 1. hook_token_info() & 2. hook_tokens() in your module file Below i have given code to create my custom token "query-param-all" and used that token in views- Textarea field.....
如果我记得... http ://api.drupal.org/api/drupal/modules--node--node.api.php/function/hook_delete/7
这有帮助吗?自从我搞乱 Drupal 以来已经有一段时间了。
要创建/提供自定义 Drupal 挂钩,您必须以某种方式实现,以便使用 module_invoke 或 module_invoke_all 调用挂钩不会与其他模块挂钩产生任何冲突。挂钩的名称应该是唯一的,并且应该以通用方式提供所有/特定功能,不需要对代码进行任何类型的调整。所有配置都必须位于管理页面上,并且应将这些配置存储在单独的表中或由 Drupal 或模块所依赖的模块创建的任何现有表中。钩子应该很容易被其他模块实现,并且实现起来不应该太复杂。当您创建自定义挂钩时,您的模块充当 API 提供者。
If i recall... http://api.drupal.org/api/drupal/modules--node--node.api.php/function/hook_delete/7
does ths help? been a while since I messed with Drupal.
To create/offer custom Drupal hook, you must implement in a ways such that calling the hook with module_invoke or module_invoke_all does not make any conflicts with other module hooks. The name of the hook should be unique and it should offer all/specific feature in such a general way that it doesn't require any type of adjustments with code. All the configuration must go on admin pages and should store those configurations in a separate table or any existing tables create by Drupal or modules on which your modules depends. The hook should be easy to implment by other modules and it should not be much complex to implement. When you create custom hooks, your module(s) act(s) as API provider.
对于 Drupal 6 和7、 drupal_alter() 可能是最好的选择。
如module_invoke_all()文档,
在 Drupal 8 中,使用 ModuleHandler::alter。
For Drupal 6 & 7, drupal_alter() is probably the best option.
As stated in the module_invoke_all() documentation,
In Drupal 8, use ModuleHandler::alter.