区分 Drupal Hook 实现和常规函数

发布于 2024-10-18 21:54:22 字数 474 浏览 0 评论 0原文

我有几个关于 Drupal 编码约定的问题,但我无法从文档或代码中找到这些问题。

除了了解 Drupal 中每个钩子的名称之外,是否有办法区分实现钩子的函数和仅为钩子提供一些功能的函数?要么通过代码强制执行,要么通过某种约定执行?

第二,可能相关的问题。在查看了核心模块之后,我注意到一些函数的命名带有前导下划线。

function _node_rankings(SelectQueryExtender $query) {
    ...
}

下划线有何含义?我的假设是它模仿“受保护”约定,这意味着它只能从 Node.module 文件中的其他函数调用;但是,我找不到任何东西可以证实这一点。

我了解 编码标准,但它们似乎针对通用 PHP 语法,而不是针对 Drupal 内部系统的约定。

I have a couple of questions on Drupal coding conventions that I haven't been able to gleam from the docs or the code.

Outside of knowing the name of every hook in Drupal, is there a way to differentiate a function that implements a hook from a function that's just providing a bit of functionality for a hook? Either something enforced via code or some convention?

Second, possibly related question. After reviewing the core modules, I've noticed that some functions are named with a leading underscore

function _node_rankings(SelectQueryExtender $query) {
    ...
}

What meaning is attached to the underscore? My assumption it's mimicking a "protected" convention, meaning that it should only be called from other functions in the node.module file; however, I couldn't find anything to confirm this.

I know about the Coding Standards, but they seem aimed at general PHP syntax, not conventions aimed at Drupal's internal systems.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无妨# 2024-10-25 21:54:22

你是对的,函数名称中的下划线前缀表示它应该被视为私有函数,只能由声明它的模块调用。

我不知道这是否在 Drupal 的官方文档中,但是 drupal.org 上有一些帖子证实了这一点(例如 this 这个)。

编辑:是的,它还可以避免将“正常”函数变成钩子实现(尽管您应该尝试不在现有钩子之后命名函数)。

You're right, the underscore prefix in function names indicates that it should be treated as a private function, only being called by the module that declared it.

I don't know if this is in Drupal's official docs, but there are some posts on drupal.org confirming this (like this or this).

EDIT: and yes, it also works to avoid turning a "normal" function into a hook implementation (although you should try to not name functions after existing hooks).

动听の歌 2024-10-25 21:54:22

在回答你的第一个问题时,如果你查看大多数模块的代码,钩子实现上面的注释通常会说:

/**
 * Implementation of hook_foo().
 */

使用 Drupal 一段时间后,你将开始识别最常见的钩子。

In answer to your first question, if you look at the code of most modules, the comments above hook implementations will typically say:

/**
 * Implementation of hook_foo().
 */

After working with Drupal a while, you'll start to recognize the most common hooks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文