php 你能在 Drupal 中创建你自己的 Hook 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4994512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you Create your Own Hook in Drupal?
提问by Alan Storm
Is it possible to create your ownhook 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 模块中创建自己的钩子供其他 Drupal 模块使用?如果没有,Drupal 中是否有第三方开发人员提供钩子的机制?如果到目前为止一切都没有,那么核心中的钩子列表在哪里实现?
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
据我了解,Drupal 模块处理类似系统的事件hooks。当您创建新模块时,您将创建实现挂钩的函数。例如,有一个hook_delete
钩子。如果你在你的模块中实现了一个函数
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 ownhooks. Say, something like hook_alanskickbutthook
so that other module developers could subscribe to this hook.
我想知道的是,作为第三方模块开发人员,有没有办法创建自己的钩子。比如说,hook_alanskickbutthook
其他模块开发人员可以订阅这个钩子。
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.
如果这是可能的,你怎么做?我查看了官方文档,并没有找到太多内容,当我开始查看 Drupal 源代码时,我仍然有点头晕(我了解递归,但没有花足够的时间考虑递归问题)。欢迎提供完整的解决方案,但我很高兴能够指出正确的方向。
回答by jpstrikesback
Module_invoke_all() is your ticket to creating your own hooks:
Module_invoke_all() 是您创建自己的钩子的门票:
see the API:
见API:
http://api.drupal.org/api/drupal/includes--module.inc/function/module_invoke_all
http://api.drupal.org/api/drupal/includes--module.inc/function/module_invoke_all
and then look at this great writeup:
然后看看这篇很棒的文章:
(edit: was at http://himerus.com/blog/himerus/creating-hooks-your-drupal-modulesbut this is now gone)
(编辑:在http://himerus.com/blog/himerus/creating-hooks-your-drupal-modules但现在已经消失了)
Once you've made your hook, it can be called in another module using:
制作钩子后,可以使用以下命令在另一个模块中调用它:
/**
* Implementation of hook_myhookname()
*/
function THISMODULENAME_myhookname(args){
//do stuff
}
回答by tyler.frankenstein
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:
例如,假设您想创建 hook_my_custom_goodness() 供其他人使用。然后只需将这样的代码放在您的模块中您希望挂钩发生的位置:
$variables['msg'] = 'foo';
// Make sure at least one module implements our hook.
if (sizeof(module_implements('my_custom_goodness')) > 0) {
// Call modules that implement the hook, and let them change $variables.
$variables = module_invoke_all('my_custom_goodness', $variables);
}
drupal_set_message($variables['msg']); // Will display 'bar' instead.
Now, if anybody wanted to use your hook, then they could do so in their own module like this:
现在,如果有人想使用你的钩子,那么他们可以在自己的模块中这样做:
/**
* Implements hook_my_custom_goodness().
*/
function SOME_OTHER_MODULE_my_custom_goodness($variables) {
$variables['msg'] = 'bar';
return $variables;
}
There is a more complete explanation here:
这里有更完整的解释:
http://tylerfrankenstein.com/code/drupal-create-custom-hook-for-other-modules
http://tylerfrankenstein.com/code/drupal-create-custom-hook-for-other-modules
回答by colan
For Drupal 6 & 7, drupal_alter()is probably the best option.
对于 Drupal 6 和 7,drupal_alter()可能是最好的选择。
As stated in the module_invoke_all() documentation,
All arguments are passed by value. Use drupal_alter() if you need to pass arguments by reference.
所有参数都按值传递。如果需要通过引用传递参数,请使用 drupal_alter()。
In Drupal 8, use ModuleHandler::alter.
在 Drupal 8 中,使用ModuleHandler::alter。
Passes alterable variables to specific hook_TYPE_alter() implementations.
将可变变量传递给特定的 hook_TYPE_alter() 实现。
回答by Brian
If i recall... http://api.drupal.org/api/drupal/modules--node--node.api.php/function/hook_delete/7
如果我记得... 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.
这有帮助吗?自从我弄乱了 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 钩子,您必须以一种方式实现,即使用 module_invoke 或 module_invoke_all 调用钩子不会与其他模块钩子产生任何冲突。钩子的名称应该是唯一的,它应该以一种通用的方式提供所有/特定的功能,不需要对代码进行任何类型的调整。所有配置都必须在管理页面上进行,并且应该将这些配置存储在单独的表或由 Drupal 创建的任何现有表或您的模块所依赖的模块中。这个钩子应该很容易被其他模块实现,实现起来也不应该太复杂。当您创建自定义钩子时,您的模块充当 API 提供者。