0

I want to open the comments for a content type by should be unpublished by default. Currently the comments are published by default.

Is there any way to accomplish this task. There is no option in the comments settings of the

2 Answers 2

6

If you don't want users to add comments without admin approval then you need to disable the below permission...

Skip comment approval

Once you disable it users would be able to post comments but they will not be visible to other users until the comment is approved by an admin.

This would set the permission for the entire contents and not for a specific content type.

If you want to set it for a particular content type then you would need to look into one of the contributed modules

commentaccess

Allow anonymous comments per node type

Comment Permissions - Stable Version only for 6 does not have a stable 7 version yet

1
  • the comment access module also enable the admin approval for all content types and the and the anonymous comments only enables posting comment anonymously.
    – Ahmad
    Commented Jan 27, 2013 at 14:00
1

You can make a custom module to do this.

<?php
/**
 * Implement hook_permission
 * Defines a new permission that can be selected per role at the page
 * admin/people/permissions
 */
function MY_MODULE_permission() {
  return array(
    'skip required comment approval' => array(
      'title' => t('Skip "Require comment approval"'),
      'description' => t('For nodes with "Require comment approval" set, this user\'s comments skip moderation'),
    ),
  );
}



/**
 * Implement hook_form_alter for BASE_FORM_ID node_form
 * This modifies all node add/edit forms
 */
function MY_MODULE_form_node_form_alter(&$form, &$form_state) {
  // Move 'Require comment approval' field into the comment settings fieldset
  if (isset($form['field_require_comment_approval'])) {
    $form['comment_settings']['field_require_comment_approval'] = $form['field_require_comment_approval'];
    unset($form['field_require_comment_approval']);
  }
}



/**
 * Implement hook_comment_presave
 * This hook runs before a comment is saved to the database, allowing us to
 * modify the comment as it is being saved
 */
function MY_MODULE_comment_presave($comment) {
  // If the parent node has the field "Require comment approval" checkbox set,
  // make the new comment's status set to "unpublished"
  if (empty($comment->cid)) {
    $node = node_load($comment->nid);
    if ($node && ($items = field_get_items('node', $node, 'field_require_comment_approval'))) {
      $item = !empty($items) ? reset($items) : array('value' => FALSE);
      if ($item['value']) {
        $account = user_load($comment->uid);
        // Comments for users with the 'skip required comment approval' permission
        // skip this step
        if ($account && !user_access('skip required comment approval', $account)) {
          $comment->status = COMMENT_NOT_PUBLISHED;
        }
      }
    }
  }
}

The code relies on a field added to your individual content types with the machine name field_require_comment_approval. This field can actually be set to true or false per node, allowing some nodes to require comment moderation and other nodes to allow comments posted to skip validation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.