You are here

function comment_notify_form_comment_form_alter in Comment Notify 8

Same name and namespace in other branches
  1. 7 comment_notify.module \comment_notify_form_comment_form_alter()

Add the comment_notify fields in the comment form.

File

./comment_notify.module, line 44
This module provides comment follow-up e-mail notifications.

Code

function comment_notify_form_comment_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $user = \Drupal::currentUser();
  if (!($user
    ->hasPermission('subscribe to comments') || $user
    ->hasPermission('administer comments'))) {
    return;
  }

  /** @var \Drupal\Core\Entity\EntityInterface $comment_entity */
  $comment_entity = $form_state
    ->getFormObject()
    ->getEntity();
  $field_identifier = CommentNotifySettings::getCommentFieldIdentifier($comment_entity);

  // Only add the checkbox if this is an enabled content type.
  $enabled_types = \Drupal::config('comment_notify.settings')
    ->get('bundle_types');
  if (!in_array($field_identifier, $enabled_types)) {
    return;
  }
  $available_options = _comment_notify_options();

  // Add the checkbox for anonymous users.
  if ($user
    ->isAnonymous()) {

    // If anonymous users can't enter their e-mail don't tempt them with the
    // checkbox.
    if (empty($form['author']['mail'])) {
      return;
    }
    $form['#validate'][] = 'comment_notify_comment_validate';
  }
  module_load_include('inc', 'comment_notify', 'comment_notify');

  /** @var \Drupal\comment_notify\UserNotificationSettings $user_settings */
  $user_settings = \Drupal::service('comment_notify.user_settings');
  $preference = $user_settings
    ->getSetting($user
    ->id(), 'comment_notify');

  // If you want to hide this on your site see http://drupal.org/node/322482
  $form['comment_notify_settings'] = [
    '#attached' => [
      'library' => [
        'comment_notify/comment_notify',
      ],
    ],
  ];
  $form['#attributes']['class'][] = 'comment-notify-form';
  $form['comment_notify_settings']['notify'] = [
    '#type' => 'checkbox',
    '#title' => t('Notify me when new comments are posted'),
    '#default_value' => (bool) $preference,
    '#attributes' => [
      'class' => [
        'comment-notify',
      ],
    ],
  ];
  $form['comment_notify_settings']['notify_type'] = [
    '#type' => 'radios',
    '#options' => $available_options,
    '#default_value' => $preference != "none" ? $preference : 1,
    '#attributes' => [
      'class' => [
        'comment-notify-type',
      ],
    ],
  ];
  if (count($available_options) == 1) {
    $form['comment_notify_settings']['notify_type']['#type'] = 'hidden';
    $form['comment_notify_settings']['notify_type']['#value'] = key($available_options);
  }

  // If this is an existing comment we set the default value based on their
  // selection last time.

  /** @var \Drupal\comment\CommentInterface $comment */
  $comment = $form_state
    ->getFormObject()
    ->getEntity();
  if (!$comment
    ->isNew()) {
    $notify = comment_notify_get_notification_type($comment
      ->id());
    $form['comment_notify_settings']['notify']['#default_value'] = (bool) $notify;
    if (count($available_options) > 1) {
      $form['comment_notify_settings']['notify_type']['#default_value'] = empty($notify) ? COMMENT_NOTIFY_ENTITY : $notify;
    }
    else {
      $form['comment_notify_settings']['notify_type']['#default_value'] = key($available_options);
    }
  }
  $form['actions']['submit']['#submit'][] = '_comment_notify_submit_comment_form';
}