如果我已经有了 hook_mail,那么还有 hook_mail_alter 有什么意义呢?

发布于 2024-09-12 11:45:28 字数 182 浏览 3 评论 0原文

如果我已经有了 hook_mail,那么还有 hook_mail_alter 有什么意义呢?

例如,我看到 hook_mail_alter 用于向我的邮件消息添加页脚。但我可以使用 hook_mail() 来添加它,而不是使用 2 个函数……我错过了什么?

也许是在调用其他函数之后添加页脚?

What's the point to have hook_mail_alter if I already have hook_mail?

For example, I saw that hook_mail_alter is used to add a footer to my mail message. But I could use hook_mail() to add it, instead of using 2 functions… What am I missing?

Maybe it is done to add the footer after some other function is invoked?

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

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

发布评论

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

评论(1

月下凄凉 2024-09-19 11:45:28

应该从模块中使用 hook_mail() 来更改其自己的邮件消息,而应该从模块中使用 hook_mail_alter() 来更改其他模块发送的消息。

drupal_mail() 中获取的以下代码可以清楚地看出这一点:

// Build the e-mail (get subject and body, allow additional headers) by
// invoking hook_mail() on this module. We cannot use module_invoke() as
// we need to have $message by reference in hook_mail().
if (function_exists($function = $module .'_mail')) {
  $function($key, $message, $params);
}

// Invoke hook_mail_alter() to allow all modules to alter the resulting e-mail.
drupal_alter('mail', $message);

$module 是传递给 drupal_mail() 的第一个参数。
很明显,该函数不会调用实现它的每个模块的 hook_mail() 实现,但它仅为调用该函数的模块调用钩子。

还有其他差异,例如调用两个钩子时(hook_mail_alter() 无法设置消息的语言,该语言是在调用 hook_mail_alter() 之前设置的),以及它们获取的参数(hook_mail($key, &$message, $params)hook_mail_alter(&$message))。

hook_mail() should be used from a module to alter its own mail message, while hook_mail_alter() should be used from a module to alter the message sent by other modules.

This is clear from the following code taken from drupal_mail():

// Build the e-mail (get subject and body, allow additional headers) by
// invoking hook_mail() on this module. We cannot use module_invoke() as
// we need to have $message by reference in hook_mail().
if (function_exists($function = $module .'_mail')) {
  $function($key, $message, $params);
}

// Invoke hook_mail_alter() to allow all modules to alter the resulting e-mail.
drupal_alter('mail', $message);

$module is the first parameter passed to drupal_mail().
It's clear the function doesn't invoke the implementation of hook_mail() of every module implementing it, but it invokes the hook just for the module calling the function.

There are other differences, such as when the two hooks are invoked (hook_mail_alter() cannot set the language for the message, which is set before hook_mail_alter() is invoked), and the parameters they get (hook_mail($key, &$message, $params) versus hook_mail_alter(&$message)).

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