Index: comment_display.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/comment_display/comment_display.module,v retrieving revision 1.2 diff -u -p -r1.2 comment_display.module --- comment_display.module 9 Nov 2008 05:09:32 -0000 1.2 +++ comment_display.module 26 Dec 2008 18:53:14 -0000 @@ -29,7 +29,7 @@ function comment_display_node_show($node } $output = node_view($node, FALSE, TRUE); - // Note: Output of comments moved into comment_display_preprocess_page(). + // Note: Output of comments moved into comment_display(). // Update the history table, stating that this user viewed this node. node_tag_new($node->nid); @@ -53,10 +53,13 @@ function comment_display_node_page_view( * Provide node comments, if available, as $comments variable to theme. */ function comment_display_preprocess_page(&$vars) { + // Prevent overriding a 'comments' region. + if (isset($vars['comments'])) { + return; + } $vars['comments'] = ''; - if (function_exists('comment_render') && !empty($vars['node']) && $vars['node']->comment) { - $arg2 = arg(2); - $vars['comments'] .= comment_render($vars['node'], ($arg2 && is_numeric($arg2) ? $arg2 : NULL)); + if (!empty($vars['node'])) { + $vars['comments'] .= comment_display($vars['node']); // Reconstruct CSS and JS variables. $vars['css'] = drupal_add_css(); @@ -65,3 +68,63 @@ function comment_display_preprocess_page } } +/** + * Implementation of hook_block(). + */ +function comment_display_block($op = 'list', $delta = 0, $edit = array()) { + if ($op == 'list') { + $blocks['comments'] = array( + 'info' => t('Comments'), + 'region' => 'content', + 'weight' => 10, + 'status' => 1, + ); + $blocks['comment_form'] = array( + 'info' => t('Comment form'), + 'region' => 'content', + 'weight' => 11, + 'status' => 1, + ); + return $blocks; + } + else if ($op == 'view') { + $node = menu_get_object(); + if (!empty($node)) { + if ($delta == 'comments') { + $block['subject'] = t('Comments'); + $block['content'] = comment_display($node); + } + else { + $block['subject'] = t('Post new comment'); + $block['content'] = drupal_get_form('comment_form', array('nid' => $node->nid)); + } + } + return $block; + } +} + +/** + * Helper function to render and return comments. + * + * @param $node + * A fully loaded node object. + */ +function comment_display($node) { + static $output; + + if (isset($output)) { + return $output; + } + $output = ''; + if (function_exists('comment_render') && $node->comment) { + $arg2 = arg(2); + $comments = comment_render($node, ($arg2 && is_numeric($arg2) ? $arg2 : NULL)); + // Testing the length is awkward, but defining an alternate template just to + // prevent the block from displaying when empty would be even more awkward. + if (strlen($comments) > 50) { + $output .= $comments; + } + } + return $output; +} +