8

Аctually I want to list comments for nodes myself (by Views, for example), so I need to hide standard comments list from core comment module. I can do it like that:

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

But it will not prevent DB query for comments.
So I found some crazy ways to trick the comments module. For example, to set fake $node->preview property, or to create new view mode and overwrite standard node/%node callback.

Is it really so difficult to hide comments below the node but still allow to add new comment, or I missed something?

Update about “Hidden” option

What about “Hidden” value for the content type:
I need close/open behaviour for my comments. But if I set “Hidden” for my content type, my new content pieces are set to “Comments closed” instead of hidden (here is the code). Another problem is that user with permission to “post comments” can't add a comment, if comments are hidden (here is the code).
So I can't get the idea.

Answer

Solution found with hook_module_implements_alter(). Thanks a lot!

  • its easy... edit the content type, under comment settings select "Hidden". this will hide your comments – subhojit777 Apr 16 '12 at 11:39
  • @subhojit777 OP want to prevent DB query i think. – niksmac Apr 16 '12 at 13:09
9

In modules/comment/comment.module we can see that hook_node_view() (comment_node_view()) is running on line 614. This is where the functions are firing to load all comments and attach them to the node. The first 2 sets of if () { ... } appear to be handling the add comment form and action link. Then the final block of code is finally adding the comments, which I believe is what you're actually most concerned about hiding, and I assume you want this skipped for performance reasons:

// 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);
}

I think this code is silly, because comment module should not be dependent on any particular hardcoded view mode. Hopefully this will change and become a setting in the UI.

Regardless, I've found a similar post that might interest you: Is there a way to stop comment_node_view from firing?

Basically you're wanting to use hook_module_implements_alter() to stop the comment module from being able to fire off hook_node_view(). Here is what you need to add to your custom module:

function hook_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'node_view') {
    unset($implementations['comment']);
  }
}
  • 1
    Thank you very much! Especially for hook_module_implements_alter! – kalabro Apr 16 '12 at 13:33
0

Just try to use hide() in your template file, or hook_preprocess_node():

hide($content['comments']);

Not the answer you're looking for? Browse other questions tagged or ask your own question.