You are here

function comment_notify_form_alter in Comment Notify 5.2

Same name and namespace in other branches
  1. 5 comment_notify.module \comment_notify_form_alter()
  2. 6 comment_notify.module \comment_notify_form_alter()
  3. 7 comment_notify.module \comment_notify_form_alter()

Insert our checkbox, add a submit button, and populate fields.

File

./comment_notify.module, line 69
This module provides comment follow-up e-mail notification for anonymous and registered users.

Code

function comment_notify_form_alter($form_id, &$form) {
  global $user;

  // Only do alter the form if it's a comment form and the user has the permission to subscribe
  if ($form_id != 'comment_form' || !user_access('subscribe to comments')) {
    return;
  }

  // Only add the checkbox if this is an enabled content type
  $node = node_load($form['nid']['#value']);
  $enabled_types = variable_get('comment_notify_node_types', array(
    $node->type => TRUE,
  ));
  if (empty($enabled_types[$node->type])) {
    return;
  }

  // If this is a POST then it is a preview and we remind people that there post isn't done yet.
  $op = isset($_POST['op']) ? $_POST['op'] : '';
  if ($op == t('Preview comment')) {
    drupal_set_message(t('ATTENTION: Your comment is NOT YET posted - please click the post button to confirm your post'));

    //extra submit button on top
    if (!form_get_errors() && (variable_get('comment_preview', COMMENT_PREVIEW_REQUIRED) == COMMENT_PREVIEW_OPTIONAL || $op == t('Preview comment') || $op == t('Post comment'))) {
      $form['submitextra'] = array(
        '#type' => 'fieldset',
        '#title' => t('Comment is not posted yet - please click post button to confirm your post'),
        '#weight' => -99,
        '#collapsible' => FALSE,
      );
      $form['submitextra']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Post comment'),
        '#weight' => -20,
      );
    }
  }
  $total_options = array(
    COMMENT_NOTIFY_DISABLED => t('No notifications'),
    COMMENT_NOTIFY_NODE => t('For all comments on this post'),
    COMMENT_NOTIFY_COMMENT => t('Just for replies to my comment'),
  );

  // Always allow disabled
  $options[] = COMMENT_NOTIFY_DISABLED;
  $options = array_merge($options, variable_get('comment_notify_available_alerts', array(
    COMMENT_NOTIFY_NODE,
    COMMENT_NOTIFY_COMMENT,
  )));
  foreach ($options as $available) {
    $available_options[$available] = $total_options[$available];
  }

  // Add the checkbox for anonymous users and set the default based on admin settings.
  if ($user->uid == 0) {

    // If anonymous user's can't enter their e-mail don't tempt them with the checkbox
    if (empty($form['mail'])) {
      return;
    }
    $form['notify'] = array(
      '#type' => 'select',
      '#title' => t('Notify me of follow-up comments posted here'),
      '#default_value' => variable_get('comment_notify_default_anon_mailalert', FALSE),
      '#options' => $available_options,
    );
  }

  // Always show the checkbox for registered users.
  // If you want to hide this on your site see http://drupal.org/node/322482
  $form['notify'] = array(
    '#type' => 'select',
    '#title' => t('Notify me of follow-up comments posted here'),
    '#default_value' => !empty($user->comment_notify_mailalert) ? $user->comment_notify_mailalert : variable_get('comment_notify_default_anon_mailalert', FALSE),
    '#options' => $available_options,
    '#description' => t('You can change the default for this field in "Comment follow-up notification settings" on <a href="!uri">your account edit page</a>.', array(
      '!uri' => url('user/' . $user->uid . '/edit'),
    )),
  );

  // If this is an existing comment we set the default value based on their selection last time.
  if ($form['cid']['#value'] != '') {
    $notify = db_result(db_query("SELECT notify FROM {comment_notify} WHERE cid = %d", $form['cid']['#value']));
    $form['notify']['#default_value'] = $notify;
  }
}