You are here

function comment_perm_form_alter in Comment Permissions 6

Same name and namespace in other branches
  1. 5 comment_perm.module \comment_perm_form_alter()
  2. 7.2 comment_perm.module \comment_perm_form_alter()
  3. 7 comment_perm.module \comment_perm_form_alter()

Implementation of hook_form_alter().

File

./comment_perm.module, line 72
Module to control commenting permissions by role and by node type.

Code

function comment_perm_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'comment_form') {
    $nid = $form['nid']['#value'];
    if (!comment_perm_access($nid)) {

      // for comment reply pages, redirect back to the node
      if (arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2))) {
        drupal_set_message(_comment_perm_access_denied_message(), 'error');
        drupal_goto('node/' . arg(2));
      }
      else {

        // for inline comment forms, such remove their fields so nothing will show
        unset($form['comment_filter']);
        foreach ($form as $key => $item) {
          if ($type = $item['#type']) {
            switch ($type) {
              case 'value':
              case 'hidden':

                // keep these
                break;
              default:

                // remove all others
                unset($form[$key]);
                break;
            }
          }
        }

        // display a message to users who can't post comments
        $form['text'] = array(
          '#value' => '<p id="comment-perm-access-denied">' . _comment_perm_access_denied_message() . '</p>',
        );
      }
    }
    if (arg(0) == 'comment' && arg(1) == 'edit' && is_numeric(arg(2))) {
      if (!comment_perm_edit_access($nid)) {
        drupal_set_message(_comment_perm_access_denied_message(), 'error');
        drupal_goto('node/' . $nid);
      }
    }
  }
}