是什么将 Drupal Hook 与特定模块联系起来?
是什么将 Drupal Hook 与特定模块联系起来?
在 Drupal 7 中,每个核心模块都有一个“api”文件,
$ ls modules/*/*.api.php
modules/aggregator/aggregator.api.php modules/openid/openid.api.php
modules/block/block.api.php modules/overlay/overlay.api.php
modules/comment/comment.api.php modules/path/path.api.php
modules/contextual/contextual.api.php modules/rdf/rdf.api.php
modules/dashboard/dashboard.api.php modules/search/search.api.php
modules/field/field.api.php modules/shortcut/shortcut.api.php
modules/field_ui/field_ui.api.php modules/simpletest/simpletest.api.php
modules/file/file.api.php modules/system/system.api.php
modules/filter/filter.api.php modules/system/theme.api.php
modules/help/help.api.php modules/taxonomy/taxonomy.api.php
modules/image/image.api.php modules/trigger/trigger.api.php
modules/locale/locale.api.php modules/update/update.api.php
modules/menu/menu.api.php modules/user/user.api.php
modules/node/node.api.php
每个文件都包含一个从未(?)调用的函数,但记录了其他模块(包括第 3 方)可以实现的挂钩的存在。
File: modules/path/path.api.php
function hook_path_delete($path) {
db_delete('mytable')
->condition('pid', $path['pid'])
->execute();
}
我的问题:是什么将特定的钩子与特定的模块联系起来?为什么 path_delete
挂钩包含在 path.api.php
文件中?为什么 entity_view
挂钩包含在 system.api.php
文件中?这是否只是任意的、事后组织的,或者 Drupal 系统中是否有某些东西将特定的钩子与特定的模块联系起来?或者其他什么?
What Ties a Drupal Hook to a Particular Module?
In Drupal 7, every core module has an "api" file
$ ls modules/*/*.api.php
modules/aggregator/aggregator.api.php modules/openid/openid.api.php
modules/block/block.api.php modules/overlay/overlay.api.php
modules/comment/comment.api.php modules/path/path.api.php
modules/contextual/contextual.api.php modules/rdf/rdf.api.php
modules/dashboard/dashboard.api.php modules/search/search.api.php
modules/field/field.api.php modules/shortcut/shortcut.api.php
modules/field_ui/field_ui.api.php modules/simpletest/simpletest.api.php
modules/file/file.api.php modules/system/system.api.php
modules/filter/filter.api.php modules/system/theme.api.php
modules/help/help.api.php modules/taxonomy/taxonomy.api.php
modules/image/image.api.php modules/trigger/trigger.api.php
modules/locale/locale.api.php modules/update/update.api.php
modules/menu/menu.api.php modules/user/user.api.php
modules/node/node.api.php
Each of these files contains a function that's never (?) called, but documents the existence of a hook that other modules (including 3rd party) can implement.
File: modules/path/path.api.php
function hook_path_delete($path) {
db_delete('mytable')
->condition('pid', $path['pid'])
->execute();
}
My question: What ties a particular hook to a particular module? Why is the path_delete
hook included in the path.api.php
file? Why is the entity_view
hook included in the system.api.php
file? Is this just arbitrary, after the fact organization, or is there something in the Drupal system that ties a particular hook to a particular module? Or something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
module_invoke()
调用挂钩 和module_invoke_all()
:如果您查看这两个函数的代码,您也许能够拼凑出它是如何工作的,但基本上,如果我将其添加到我的模块的代码中:Drupal 将调用 hook_foo_bar 的每个实现($var1, $var2) 它在启用的模块中找到。基于此,您应该看到,唯一将特定钩子与特定模块联系起来的是命名约定:如果我调用我的模块
foo
,我的钩子函数应该以hook_foo_
开头代码>.您关于
*.api.php
被调用的任何内容都是正确的:由于模块调用只是一个函数调用,模块作者仅出于文档目的而包含foo.api.php
通知实现者如何实现钩子。例如,在上面的情况下,
foo.api.php
将包含一个示例函数,例如:但作为模块实现者,我可以以不同的方式实现
hook_foo_bar()
:当
module_invoke_all()
被调用时,Drupal 将使用实现模块的短名称 (mymodule
) 和传递给module_invoke_all()< 的钩子名称来创建一个函数/code> (
foo_bar
),从而调用我刚刚定义的函数mymodule_foo_bar()
。核心中的
system
模块有点包罗万象:Drupal 8 的一项任务是 杀死它并将其功能委托给其他模块。Hooks are invoked using
module_invoke()
andmodule_invoke_all()
: if you look at the code for those two functions, you might be able to piece together how it works, but basically, if I add this to my module's code:Drupal will invoke every implementation of
hook_foo_bar($var1, $var2)
it finds in enabled modules. Based on this, you should see that only thing that ties a particular hook to a particular module is a naming convention: if I call my modulefoo
, my hook functions should begin withhook_foo_
.You are correct about nothing in
*.api.php
being called: since a module invocation is just a function call, module authors includefoo.api.php
merely for documentation purposes to inform implementors how to implement the hook.For example, in the case above,
foo.api.php
would include a sample function like:But as a module implementor, I could implement
hook_foo_bar()
in a different fashion:And when
module_invoke_all()
gets called, Drupal will craft a function using the implementing module's short name (mymodule
) and the hook name passed tomodule_invoke_all()
(foo_bar
), thus calling the functionmymodule_foo_bar()
I just defined.The
system
module in core is a bit of a catch-all: one task for Drupal 8 is to kill it off and delegate its functionality to other modules.也许你可以尝试学习 drupal 的 hooking ?这很简单:
http://api.drupal.org/api/ drupal/includes--module.inc/group/hooks/7
关于hook_path_delete:
看看path.module,你会在某个地方看到path_delete(...) 调用。
例如,在 path_node_update() 中 - 调用此函数然后更改当前路径 - 它会删除之前的旧路径,然后为节点创建新路径。
现在查看定义的 path_delete() 函数的位置 - 它放置在 path.inc 文件中:
在此函数中,您将看到: module_invoke_all('path_delete', $path); - 这个功能有什么作用?
它列出了定义了 hook_path_delete 的所有模块(在 drupal 7 中它被缓存)(如我之前所示,对于 yiu 自定义模块,它定义为 YOURMODULENAME_path_delete,它也将包含在这里),并一一运行所有这些函数(运行顺序由模块的权重和文件名定义)。
那么现在您可以在自定义模块中做什么?您可以响应此删除反应并执行一些其他操作 - 例如,删除可用于复制该节点路径的其他路径(这只是示例)。
PS
启动自定义模块的好点:http://drupal.org/contributors-guide
may be you can try learn hooking for drupal? it's easy:
http://api.drupal.org/api/drupal/includes--module.inc/group/hooks/7
About hook_path_delete:
Look path.module, you'll see somewhere path_delete(...) calls.
For example, in path_node_update() - this function called then you change current path - it's delete old path before, than create new path for node.
Now looks where defined path_delete() function - it's placed in in path.inc file:
In this function you'll see: module_invoke_all('path_delete', $path); - what this function do?
It's list all modules (in drupal 7 it's cached) where defined hook_path_delete (as i show before, for yiu custom module it defined as YOURMODULENAME_path_delete, and it'll also included here), and run all these functions one by one (order of running defined by weight and filename of modules).
So now what can you do in custom module? You can respond on this delete reaction and do some other actions - for example, delete other path that can be used for duplicating path of that node (it's just example).
p.s.
Good point for starting custom modules: http://drupal.org/contributors-guide