发表评论时如何覆盖drupal默认消息?
我想覆盖发布评论的默认消息。
例如,我想显示“您的评论已发送给网站管理员,并将保持私密。”而不是“您的评论已排队等待网站管理员审核,并将被保留”批准后发布。”
我尝试了挂钩插入,但它没有覆盖该消息:
function custom_comment_insert($comment) {
//drupal_get_messages(null, true);
unset($_SESSION['messages']);
drupal_set_message(t('override like this.'));
}
I would like to overwrite a default message for posting a comment.
For example, I would like to display "Your comment has been sent to the site moderator and will remain private." instead of "Your comment has been queued for review by site administrators and will be published after approval."
I tried hook insert, but it didn't override the message:
function custom_comment_insert($comment) {
//drupal_get_messages(null, true);
unset($_SESSION['messages']);
drupal_set_message(t('override like this.'));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要使用它,而是使用字符串覆盖来更改消息。一般来说,如果你想重写文本,不要修改它,而是覆盖它。
在 Drupal 7 中,您可以使用
settings.php
直接更改它:(参见 http://preprocess.me/drupal-override-strings-in-settingsphp)Don't use that, use String Overrides to change the message instead. In general, if you want to reword text, don't hack it, override it.
In Drupal 7, you can use
settings.php
to change it directly: (See http://preprocess.me/drupal-override-strings-in-settingsphp)@Maciej Zgadzaj 你的解决方案也工作得很好。
我在 hook_form_alter http://bit.ly/12u09O 上找到了一个有用的教程
@Maciej Zgadzaj Your solution works fine as well.
I found a useful tutorial on hook_form_alter http://bit.ly/12u09O
保存新评论后,该消息将添加到
comment_form_submit()
中的会话中。因此,要么更改评论表单,在主表单之后添加自己的提交功能,然后删除那里的消息(这似乎是一个更好的主意,因为这样只有在真正发布新评论时才会执行此操作),或...
实际上,没有“或”。最初想建议像
hook_init()
这样的东西作为替代方案,但是不,你不想把它放在那里。 ;)The message is being added to session in
comment_form_submit()
, AFTER new comment is saved.So either you alter comment form, add your own submit function after the main one, and remove the message there (which seems to be a better idea, as this way this is going to be done only when a new comment is really posted), or...
Actually, no 'or'. Originally wanted to suggest something like
hook_init()
as an alternative, but no, you don't want to put it there. ;)Drupal Answers SE 其中给出的解决方案是使用
array_search()
搜索实际的消息数组,然后比较翻译后的字符串以识别要更改的字符串的键。然后展示如何更改它。当作为模块的一部分实现时,该解决方案有效,而更改设置文件中的消息字符串则无效。
There is question over at Drupal Answers SE where the solution given is to use
array_search()
to search the actual message array, and then compare the translated strings to identify the key of the string to alter. Then showing how to alter it.This solution works when implemented as part of a module, while changing the message string in the setting file does not.