欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

如何隐藏节点下方的评论 comment comments ,但不关闭它们? 有大用

实际上,我想自己列出节点的评论(例如,通过视图),所以我需要从核心评论模块中隐藏标准评论列表。我可以这样做:

  unset($node['comments']['comments']);

但它不会阻止数据库查询评论。
所以我找到了一些疯狂的方法来欺骗评论模块。例如,设置假$node->preview属性,或创建新的视图模式并覆盖标准node/%node回调。

在节点下方隐藏评论真的那么难,但仍然允许添加新评论,或者我错过了什么?

关于“隐藏”选项的更新

内容类型的“隐藏”值怎么样:
我的评论需要关闭/打开行为。但是如果我为我的内容类型设置“隐藏”,我的新内容片段将设置为“评论关闭”而不是隐藏(这是代码)。另一个问题是拥有“发表评论”权限的用户无法添加评论,如果评论被隐藏(这里是代码)。
所以我想不通。

回答

找到的解决方案hook_module_implements_alter()非常感谢!

  • 很简单...编辑内容类型,在评论设置下选择“隐藏”。这将隐藏您的评论  2012 年 4 月 16 日,11:39
  • @subhojit777 OP 我认为要阻止数据库查询。  2012 年 4 月 16 日,13:09
9

在 modules/comment/comment.module 中,我们可以看到 hook_node_view() (comment_node_view()) 正在第 614 行运行。这是函数触发加载所有评论并将它们附加到节点的地方。前 2 组 if () { ... } 似乎正在处理添加评论表单和操作链接。然后最后的代码块终于添加了注释,我相信这是您实际上最关心隐藏的内容,并且我假设您出于性能原因希望跳过它:

// Only append comments when we are building a node on its own node detail
// page. We compare $node and $page_node to ensure that comments are not
// appended to other nodes shown on the page, for example a node_reference
// displayed in 'full' view mode within another node.
if ($node->comment && $view_mode == 'full' && node_is_page($node) && empty($node->in_preview)) {
  $node->content['comments'] = comment_node_page_additions($node);
}

我认为这段代码很愚蠢,因为注释模块不应该依赖于任何特定的硬编码视图模式。希望这会改变并成为用户界面中的设置。

无论如何,我发现了一篇您可能感兴趣的类似帖子:有没有办法阻止 comment_node_view 被触发?

基本上你想使用 hook_module_implements_alter() 来阻止评论模块能够触发 hook_node_view()。以下是您需要添加到自定义模块的内容:

function hook_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'node_view') {
    unset($implementations['comment']);
  }
}
  • 1
    非常感谢!特别适合hook_module_implements_alter  2012 年 4 月 16 日,13:33
0

只需尝试在模板文件中使用 hide() 或 hook_preprocess_node():

hide($content['comments']);

来自 https://drupal.stackexchange.com/questions/28428/how-to-hide-comments-below-the-node-but-not-close-them


普通分类: