php – Drupal连接到特定模块的关系是什么?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Drupal连接到特定模块的关系是什么?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
将Drupal连接到特定模块的关系是什么

在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

这些文件中的每一个都包含一个从不(?)调用函数,但记录了其他模块(包括第三方)可以实现的钩子的存在.

File: modules/path/path.api.PHP
function hook_path_delete($path) {
  db_delete('mytable')
    ->condITion('pid',$path['pid'])
    ->execute();
}

我的问题:特定钩子与特定模块的关系是什么?为什么path.deite钩子包含在Path.api.PHP文件中?为什么entity_view挂钩包含在system.api.PHP文件中?在事实组织之后,这是否是武断的,或者Drupal系统中是否存在将特定挂钩与特定模块联系起来的东西?或者是其他东西?

解决方法

使用 module_invoke()module_invoke_all()调用挂钩:如果查看这两个函数代码,您可以将它的工作原理拼凑在一起,但基本上,如果我将它添加到我的模块的代码中:

// Other code

$foo = module_invoke_all('foo_bar',$VAR1,$var2);

// More code

Drupal将调用它在启用模块中找到的hook_foo_bar($var1,$var2)的每个实现.基于此,你应该看到只有特定钩子与特定模块绑定的东西才是命名约定:如果我调用我的模块foo,我的钩子函数应该以hook_foo_开头.

你在* .api.PHP调用时没有任何意义:由于模块调用只是一个函数调用,模块作者包括foo.api.PHP仅仅是出于文档目的,通知实现者如何实现钩子.

例如,在上面的例子中,foo.api.PHP将包含一个示例函数,如:

/**
 * Doxygen comments documenting the function goes here
 */
function hook_foo_bar($var1,$var2) {
  return $var1 + $var2;
}

但作为模块实现者,我可以以不同的方式实现hook_foo_bar():

function mymodule_foo_bar($var1,$var2) {
  return $var1 - $var2;
}

调用module_invoke_all()时,Drupal将使用实现模块的短名称(mymodule)和传递给module_invoke_all()(foo_bar)的钩子名称来创建一个函数,从而调用我刚刚定义的函数mymodule_foo_bar().

核心系统模块有点全面:Drupal 8的一项任务kill it off,并将其功能委托给其他模块.

脚本宝典总结

以上是脚本宝典为你收集整理的php – Drupal连接到特定模块的关系是什么?全部内容,希望文章能够帮你解决php – Drupal连接到特定模块的关系是什么?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。