如果我已经有了 hook_mail,那么还有 hook_mail_alter 有什么意义呢?
如果我已经有了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该从模块中使用
hook_mail()
来更改其自己的邮件消息,而应该从模块中使用hook_mail_alter()
来更改其他模块发送的消息。从
drupal_mail()
中获取的以下代码可以清楚地看出这一点:$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, whilehook_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()
:$module
is the first parameter passed todrupal_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 beforehook_mail_alter()
is invoked), and the parameters they get (hook_mail($key, &$message, $params)
versushook_mail_alter(&$message)
).