Drupal snippet: moving node comments onto a block

**Update: ** Disregard what I said here, follow this instead: http://drupaleasy.com/blogs/ericmarden/2008/09/moving-comment-form-witho...

I decided to start documenting little bits of code for Drupal, so I could refer back to it when needed, and so I could have a little variety, as not everyone is interested in all the games I play. I hope this will be of some use to some of you.

I wanted to separate the comments section of my blog posts from the main content area because my layout's footer is perfect for it. But digging around for hooks / theme overrides got me nowhere. I resorted to hacking up some jQuery to grab the comments section,and prepend it to the footer. It worked well for the most part, but reCAPTCHA wouldn't play nice with this implementation.

I found out that the comments section was appended right after calling node_view, using the node_show function. The node_show function calls node_view, then adds in the comments, then updates the history tag table.

Now this is still a hack, and its arguably worse than the JS solution as it involves editing a core module. But I couldn't find anything to override this. Anyways, open up node.module, and find the node_show function. Comment out this bit of code.

  if (function_exists('comment_render') && $node->comment) {
    $output .= comment_render($node, $cid);
  }

This should disable the comments for all nodes. Now go into Blocks and create a new block. Set the input filter to PHP code, and put this code on the block body (enclose it in php tags):


if(arg(0) == 'node' && is_numeric(arg(1))&& !arg(2)) {
  $t_node = node_load(arg(1));
  if (function_exists('comment_render') && $t_node->comment) {  
    print comment_render($t_node);
  }
}

Save the block, and put it on your region of choice (mine's on content bottom), and you're done. The code should be self-explanatory. I haven't found any conflicts with this, reCAPTCHA works great now, and submitting, editing and deleting replies work just fine.

Of course this is still hackish, if anyone knows of a better way please let me know. :)

Trackback URL for this post:

http://www.chronnus.com/trackback/68
Posted in on Nov 16, 2008