Comment Moderation: The finale - Putting it all together

This is the last post in a 4 part series about Views, Views Bulk Operations, Actions and Features. In the first post of the series I walked through the Views Bulk Operations module. In the second post, a high level plan was put into place for the comment moderation feature. We also created the action plan that we followed for the final two posts. In the third post, we implemented several parts of our action plan. This included the view, creating the feature and coding the actions.

This post will finish up the comment moderation feature. Below is the action list. The bold items are addressed in this post.

Create View with 2 page panes.

This is the last post in a 4 part series about Views, Views Bulk Operations, Actions and Features. In the first post of the series I walked through the Views Bulk Operations module. In the second post, a high level plan was put into place for the comment moderation feature. We also created the action plan that we followed for the final two posts. In the third post, we implemented several parts of our action plan. This included the view, creating the feature and coding the actions.

This post will finish up the comment moderation feature. Below is the action list. The bold items are addressed in this post.

Create View with 2 page panes.

  • Approval Queue
    • Fields: Subject, Author, email address, IP, Node Title, Post Time, edit and moderate links.
    • Actions: Delete, Delete and block email, Publish, Report to Mollom and Delete.
    • Exposed Filters: Email, IP
  • Published Comment
    • Fields: Subject, Author, email, Node title, Post Time, edit
    • Actions: Delete, Delete and block email, unpublish, report to Mollom and delete, report to Mollom and unpublish.
    • Exposed Filers: Email, IP
  • Configuration:
    • Mollom
    • Permissions
    • Require fields on comment form
  • Custom Code:
    • Expose IP address and Email to views.
    • Block comments by Email.
    • Update comment email address for register users.

Update comment email address for registered users

As you recall in the past post, we gave comment email field the ability to be shown in a view. But we ran into an issue where a registered user's email address was not being posted to the mail field on the comments table. This caused the field to be blank in the view. To modify the behavior when the comment is saved, we want to use hook_comment(). Looking at the api, we want to use the 'insert' op to run our code when a comment has been inserted into the database.

/**
 * Implementation of hook_comment
 */

function comments_feature_comment(&$a1, $op) {
  if ($op == 'insert') {
    // Since register users do not sent the email to the comments table
    // We get the email address and add it to the form array
    if (!$a1['mail'] && $a1['uid']) {
      // We have to convert it to a number because it comes accross as a
      // string and the user_load modules requires a number.
      $user = user_load(intval($a1['uid']));
      // Update the comment row since it has already been saved
      db_query("UPDATE {comments} set mail = '%s' WHERE cid = %d",
        $user->mail, intval($a1['cid']));
      // Set the mail
      $a1['mail'] = $user->mail;
    }
  }
}

A good way to view the content of the arguments in Drupal hooks is to use the dpm'()function from the devel module. So I did the following to see what the elements of the $a1 array.

/**
 * Implementation of hook_comment
 */

function comments_feature_comment(&$a1, $op) {
  if ($op == 'insert') {
    dpm($a1);
  }
}

This will produce a screenshot similar to this. There are several great functions in the devel module that can help debug your code. Be sure to check out the other functions.

Block comments by Email

When we created the action to block emails, we added the list of emails to the variable $comments_blocked_emails. Now we need to use that list of emails to block them from posting comments. Since we already have the function for hook_comment, we just need to add to the function.

/**
 * Implementation of hook_comment
 */

function comments_feature_comment(&$a1, $op) {
  if ($op == 'insert') {
    // Since register users do not sent the email to the comments table
    // We get the email address and add it to the form array
    if (!$a1['mail'] && $a1['uid']) {
      // We have to convert it to a number because it comes accross as a
      // string and the user_load modules requires a number.
      $user = user_load(intval($a1['uid']));
      // Update the comment row since it has already been saved
      db_query("UPDATE {comments} set mail = '%s' WHERE cid = %d",
        $user->mail, intval($a1['cid']));
      // Set the mail
      $a1['mail'] = $user->mail;
    }
   
    // Check to see if the email is in the ban list.
    $emails = variable_get('comments_blocked_emails', array());
    if (in_array($a1['mail'], $emails)) {
      // Delete a comment and its replies:
      module_load_include('inc', 'comment', 'comment.admin');
      _comment_delete_thread($a1);
      _comment_update_node_statistics($a1->nid);
      cache_clear_all();
      drupal_set_message(t('Your comment is not allowed'), 'info');
    }
  }
}

Configuration

Now that we have all of the custom coding complete, we need to finish off with a few configuration steps. We will be configuring Mollom, the required fields on the comment form, and the comment permissions. Order is important for these settings. You must do the permissions before the comment fields

Mollom

  1. Go to the Mollom administration page (admin/settings/mollom). If you haven't crated a Mollom account, follow the on-screen instructions to activate Mollom.
  2. Change the "Protect comment form" option to "Text analysis and CAPTCHA backup."

Permissions

  1. Go to the permission page (admin/user/permissions).
  2. Scroll down to the comment module area and select the following permissions for anonymous users.
    • access comments
    • post comments
    • post comments without approval (this one is one your discretion)

Comment Fields

  1. This setting is per content type so head to the edit page for the content type.
  2. Expand the "Comment settings" fieldset.
  3. Change the "Anonymous commenting" option to "Anonymous posters must leave their contact information."

Now we have all of the configuration options set that are required to work with this module. As a reminder, you should also adjust the settings to what works best for your site.

Conclusion

I hope you learned something from this blog series. We covered a large range of Drupal topics and showed how they interact with each other. You can follow the same ideas to create content administration pages for all aspects of your site.

As I stated in the first post, this feature will be released. Stay tuned to this blog for a post announcing our public features server!

Neil Hastings