Drupal Tweaks: A better comment moderation system for Drupal 6 using form_alter on the default comment administration form

| | 3 min read

The default comment moderation system in Drupal 6 is crude at best primarily because of the limitation of the admin comment listings. The information that is shown by default are 'Subject', 'Author', 'Posted in', 'Time' and 'Operations'. Now the main objective of comment moderation is to reject spam comments. None of these fields give any information about whether the comment is a spam comment or not and you will have to view each comment by going to the edit view of the comment and then approve or reject the comment. This is a very laborious and frustrating process. You have three options to solve this problem and get a better comment administration interface - one is to create a comment view, another is to alter the default comment administration form and the third is of course a custom solution for comment administration.

In the Drupal comment administration system - the comment administration listing (admin/content/comment) and the comment approval queue listing (admin/content/comment/approval) are both generated from the comment_admin_overview form. The form can be altered using hook_form_alter. The form is themed using theme_comment_admin_overview theme function. This can be overridden via a call to hook_theme_registry_alter.

The following snippet of code does this for you. The output will be a comment administration page where you can see the full html view of the comment (through check_plain) along with the commenter's email address and the commenter's website. This much information should allow you to make a call on whether the comment is a spam comment or a genuine comment. So with this hack you will be able to approve all the non-spam comments in bulk and report/delete all the spam comments in bulk right from the comment administration page.

/**
 * Implementation of hook_form_alter().
 */
function custommodule_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'comment_admin_overview':
      $cids = array_keys($form['subject']);
      $result = db_query('SELECT c.cid, c.pid, c.nid, c.subject,
c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid,
u.name AS registered_name, u.signature, u.signature_format, u.picture,
u.data, c.status FROM {comments} c INNER JOIN {users} u ON c.uid =
u.uid WHERE c.cid IN ('. implode(',', $cids) .')');
      while ($comment = db_fetch_object($result)) {
        $author_info = check_plain($comment->mail);
        if ($author_info != '' && $comment->homepage != '') {
          $author_info .= ' | '. check_plain($comment->homepage);
        }
        if ($author_info != '') {
          $author_info = '('. $author_info .')';
        }
        $form['subject'][$comment->cid]['#value'] =  
'<b>Subject: </b>'. check_plain($comment->subject) . '<br />'.               
'<b>Author: </b>'. check_plain($comment->name) .' '. $author_info .'<br />'.    
'<b>Comment: </b><blockquote>'.check_plain($comment->comment) . '</blockquote>'.            
'<b>Posted at: </b>'.$form['node_title'][$comment->cid]['#value'] .'<br />'.  
'<b>Posted on: </b>'. format_date($comment->timestamp, 'small');
      }
      unset($form['node_title']);
      unset($form['header']['#value'][3]);
      unset($form['username']);
      unset($form['header']['#value'][2]);
      unset($form['timestamp']);
      unset($form['header']['#value'][1]);
      $form['header']['#value'][4]['data'] = 'Comment';
      break;
  }
}

/**
 * Implementation of  hook_theme_registry_alter().
 */
function custommodule_theme_registry_alter(&$theme_registry) {
  $theme_registry['comment_admin_overview']['function'] =
'theme_custommodule_comment_admin_overview';
}

/**
 * Custom theme the comment admin form.
 */
function theme_custommodule_comment_admin_overview($form) {
  $output = drupal_render($form['options']);
  if (isset($form['subject']) && is_array($form['subject'])) {
    foreach (element_children($form['subject']) as $key) {
      $row = array();
      $row[] = drupal_render($form['comments'][$key]);
      $row[] = drupal_render($form['subject'][$key]);
      $row[] = drupal_render($form['operations'][$key]);
      $rows[] = $row;
    }
  }
  else {
    $rows[] = array(array('data' => t('No comments available.'), 'colspan' => '3'));
  }
  $output .= theme('table', $form['header']['#value'], $rows);
  if ($form['pager']['#value']) {
    $output .= drupal_render($form['pager']);
  }
  $output .= drupal_render($form);
  return $output;
}