在 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']);
}
}