Creating posting guidelines for comments

Drupal allows creating posting guidelines for content types but not for comments. Countless weblogs and web columns today have comment posting guidelines and I’ve always been surprised that Drupal doesn’t have built-in support for it.

I’d like to see comment guidelines get into Drupal core but I needed a solution right away and starting writing a custom comment_guidelines module. I was almost done with a first draft of the module when I realized that it was much easier to override the comment form using template.php.

1. Create a “Comment guidelines” block

Create a custom block and add your comment guidelines to the block body. Make note of the “delta” (the unique identifier for each block) for your custom block and replace the “1” in the code below with the delta of your custom block.

The delta is the number at the end of the URL you see when configuring the custom block, so you can save your block and edit it again to see the delta in the URL.

You don’t need to enable the block for this tutorial to work.

2. Override the comment form

Add this code to your theme’s template.php file. If your theme doesn’t have one and you need help, there is a Drupal handbook page to get you started.

/**
 * Display posting guidelines at the top of comment submission forms.
 * It is useful for helping or instructing your users.
 */

function phptemplate_comment_form($form) {
  $block = module_invoke(‘block’, ‘block’, ‘view’, ‘1’);
  // replace the ‘1’ with the delta of of your
  // custom "Comment guidelines" block.
  $output .= ‘<div>’;
  $output .= ‘<div class="comment_help">’. $block[‘content’] .‘</div>’;
  $output .=  drupal_render($form);
  $output .= ‘</div>’;
  return $output;
}

While it’s possible to add comment guidelines directly to the template.php file, I decided to have template.php look for and display the contents of a custom “Comment guidelines” block. This block can be easily modified within Drupal so you (or your clients) don’t have to edit any PHP code whenever your guidelines change. Just update them at Administer > Site building > Blocks > ‘Comment guidelines’ block.

3. Start posting comments

You can see what it looks like by leaving a comment below!