You are here

function _comment_notify_submit_comment_form in Comment Notify 8

Additional submit handler for the comment form.

1 string reference to '_comment_notify_submit_comment_form'
comment_notify_form_comment_form_alter in ./comment_notify.module
Add the comment_notify fields in the comment form.

File

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

Code

function _comment_notify_submit_comment_form(array &$form, FormStateInterface $form_state) {
  module_load_include('inc', 'comment_notify', 'comment_notify');

  /** @var \Drupal\comment\CommentInterface $comment */
  $comment = $form_state
    ->getFormObject()
    ->getEntity();
  $user = \Drupal::currentUser();

  /** @var \Drupal\comment_notify\UserNotificationSettings $user_settings */
  $user_settings = \Drupal::service('comment_notify.user_settings');

  // If the form component is visible, these values should be in the form state.
  // Otherwise, use the user's preferences.
  if ($form_state
    ->hasValue('notify') && $form_state
    ->hasValue('notify_type')) {
    $status = $form_state
      ->getValue('notify') ? $form_state
      ->getValue('notify_type') : COMMENT_NOTIFY_DISABLED;

    // Update user's preference.
    // @todo are the user notifications allowed to be edited here?
    if (!$user
      ->isAnonymous()) {

      /** @var \Drupal\comment_notify\UserNotificationSettings $user_settings */
      $user_settings
        ->saveSettings($user
        ->id(), NULL, $status);
    }
  }
  else {
    $status = $user_settings
      ->getSetting($user
      ->id(), 'comment_notify');
  }

  // Save notification settings.
  if (comment_notify_get_notification_type($comment
    ->id())) {

    // Update existing record.
    comment_notify_update_notification($comment
      ->id(), $status);
  }
  else {

    // For new comments, we first build up a string to be used as the identifier
    // for the alert. This identifier is used to later unsubscribe the user or
    // allow them to potentially edit their comment / preferences if they are
    // anonymous. The string is built with token and their host and comment
    // identifier. It is stored and referenced, we really just need something
    // unique/unguessable. See comment_notify_unsubscribe_by_hash().
    // @todo make the comment_notify_add_notification() generate the hash.
    $hostname = !$comment
      ->getHostname() ? $comment
      ->getHostname() : (isset($user->hostname) ? $user->hostname : '');
    $notify_hash = \Drupal::csrfToken()
      ->get($hostname . $comment
      ->id());
    comment_notify_add_notification($comment
      ->id(), $status, $notify_hash, $comment->notified);
  }
}