4

I am building a site in Drupal, where there are 2 different types of registered users. Let us call them basic users and premium users. These could be roles or groups or something...

Authorized users will be able to publish pages with simple content and comments underneath. All pages are viewable publicly, even by non registered users and so are the comments.

Only registered users should be able to post comments.

What I would like is for some pages to allow comments only from premium users. So, there would be two categories of pages. Again let us call them basic pages and premium pages.

The permissions would be as follows:

Basic pages
READ: all
COMMENT: basic users, premium users

Premium pages
READ: all
COMMENT: premium users

Any help/pointers would be appreciated, as I have limited experience with Drupal.

  • Kostas, I was contemplating if there was a "no-code" solution that could be implemented with Panels, but I need to verify first this doesn't have security implications. So for now, I say WestieUK's answer is good. – Letharion Jan 9 '12 at 12:22
  • A no-code solution would be perfect ;-) – Kostas Jan 9 '12 at 13:02
  • Is this for Drupal 6 or 7? – WestieUK Jan 9 '12 at 13:21
  • It could be either one, the project is still in the planning phase. – Kostas Jan 9 '12 at 14:05
3

You can perform a hook_form_alter on the comment form in a custom module. This will allow you to change the form so it checks if the current user has a premium role. If the acting user has the premium role the option to submit a comment is displayed. If they do not show a message like "only premium members can comment".

MODULENAME_form_alter(&$form, &$form_state, $form_id) {
if ( $form_id == 'comment_form') {
  global $user;
   if (!user_role('Premium'){
    // Form alter here to unset comment form and show "only premium members can comment"
    unset($form['author']); 
    unset($form['subject']); 
    unset($form['comment_body']);
    unset($form['actions']['submit'] );
    unset($form['actions']['preview'] );
    print "Only Premium members can comment";
  } 
 }
};

You can look in comment.module for the default code for comments....

  • Thanks, that sounds... well, complicated. Lots of reading ahead! – Kostas Jan 9 '12 at 11:54
  • I would up-vote if it the code example was a bit more extensive, showing the ['#access'] = FALSE, at least. – Letharion Jan 9 '12 at 12:21
  • On second thought, I think it would be best to unset the comments form if not a premium role. edited code above to reflect this. If someone knows a better way please contribute – WestieUK Jan 9 '12 at 13:18
  • Ok, after a lot of reading and playing around with drupal, mostly 7, I am almost there. WestieUK's code does not seem to work in 7 (no such hook) and even in 6, it would only hide the comment form. That can also be done with a node-(-){$node->type).tpl.php (7: node--premium.tpl.php, 6: node-premium.tpl.php). I want to block comments, so that it is not possible to post comments (not even script kiddies). Not there yet... – Kostas Jan 12 '12 at 8:13
  • Sorry it uses hook_form_alter which should still exist in 7. You have to add the form id (you can find via firebug) updated above. Please see: api.drupal.org/api/drupal/modules--system--system.api.php/… --- Also I found this: digett.com/blog/05/26/2011/how-theme-comment-form-drupal-7 which I think will be very helpful. – WestieUK Jan 12 '12 at 9:59
3

The Comment Permissions module might be what you're looking for. Though there is only a Drupal 6 variety. Here is a quote about it (from the module's project page):

... enables control of commenting by user role and by node type. Additional user permissions for selected node types are added to the user access system so you can configure commenting with more control than Drupal core provides.

Note that "reply" links below comments may still appear for users without the permission to add comments. This is unfortunate, but I haven't found a workaround. See https://www.drupal.org/node/185855 for more information. Of course, the links can easily be removed in the theme layer.

3

Just a clean code to complete the Approved answer above:

/**
 * Implementation of hook_form_alter
 */
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
    // First make sure we are altering the right form       
    if ($form_id == 'comment_form') {
        // Keep the form alter clean by moving the logic to a separate function
        _MYMODULE_restrict_comments($form, $form_state);
    }
}

/**
 * Restrict comments in content type based on user role
 */
function _MYMODULE_restrict_comments(&$form, &$form_state) {
    global $user;
    $node = $form['#node'];
    // a custom function to do your logic
    if (_MYMODULE_comments_should_be_restricted($user, $node)) {
        // unset form elements, you can fetch them easily by print_r(array_keys($form));
        unset($form['comment_body'], $form['actions'], $form['author'], $form['subject']); // etc.
        // add a optional message for user.
        $form['no_access'] = array(
            '#markup' => t('You have no access to comment on this content.')
        );
    }
}
  • 1
    Thanks, that print in a hook, and without t() wrapping! But you should be checking for $form_id == 'comment_form', remember this is going to fire for every form. – Duncanmoo Feb 4 '16 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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