Submitted by Jeff Geerling on January 4, 2012 - 2:04pm
Most of the time, Drupal's convention of printing comments and the comment form inside the node template (node.tpl.php) is desirable, and doesn't cause any headaches.
However, I've had a few cases where I wanted to either put comments and the comment form in another place on the page, and in the most recent case, I asked around to see what people recommended for moving comments out of the normal rendering method. I found a few mentions of using Panels, and also noticed the Commentsblock module that does something like this using Views.
However, I just wanted to grab the normal comment information, and stick it directly into a block, and put that block somewhere else. I didn't want Views' overhead, or to have to re-theme and tweak things in Views, since I already have a firm grasp of comment rendering and form theming with the core comment display.
So, I set out to do something similar to this comment on drupal.org (which was also suggested by Jimajamma on Drupal Answers).
First, I had to hide the comments from the normal rendering pipeline in node.tpl.php, which involved using template_preprocess_node() to set 'comment' to 0, and a check in node.tpl.php to make sure $content['comments'] would only be rendered if $comment evaluated to TRUE:
<?php
function THEMENAME_preprocess_node(&$variables) {
// For note nodes, disable comments in the node template.
if ($variables['type'] == 'note') {
$variables['comment'] = 0;
}
}
?>Then, I simply built a block in my custom module, and used the magic of comment_node_page_additions() to render the comments and comment form, just as they would render under the node, except in my own, spiffy comment block:
<?php
/**
* Implements hook_block_info().
*/
function MODULENAME_block_info() {
$blocks['note_comments'] = array(
'info' => t('Note Comments'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function MODULENAME_block_view($delta = '') {
global $user;
$block = array();
if ($delta == 'note_comments') {
// Get the active menu object.
if ($node = menu_get_object()) {
// Make sure user is viewing a note.
if ($node->type == 'note') {
$block['content'] = '';
// Set the title of the block.
$block['subject'] = NULL;
// Render the comments and comment form (access checks, etc. are done
// by comment_node_page_additions()).
$block['content'] .= drupal_render(comment_node_page_additions($node));
}
}
}
return $block;
}
?>Then, after a quick trip to the Configure > Blocks page, where I assigned my block to a region, I had a slick comments block that I could render anywhere!
Comments
First, I had to hide the
Permalink Submitted by leschekfm (not verified) on January 4, 2012 - 6:50pm.
If you overwrite the values stored in $content['comments'] to 0 you could also just unset it. This way you wouldn't need another check in your node.tpl.php.
Or did I miss something here?
Presumably he is using the
Permalink Submitted by nicl (not verified) on January 5, 2012 - 3:56am.
Presumably he is using the same node.tpl.php template for many things - and only for some should the comments be displayed differently.
Correct; but I could still do
Permalink Submitted by Jeff Geerling on January 5, 2012 - 7:22am.
Correct; but I could still do it a bit differently. I just like simple if/endif statements in the template files. I also like sticking to one node.tpl.php with a tiny bit extra logic, rather than three or four template files, if I'm just making a tiny one line tweak.
Check out my personal website: www.lifeisaprayer.com.
Add new comment