You are here

public function AnonymousSubscriptionAccessForm::submitForm in Mailing List 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/AnonymousSubscriptionAccessForm.php, line 99

Class

AnonymousSubscriptionAccessForm
Builds the form to request access to own subscriptions for anonymous users.

Namespace

Drupal\mailing_list\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Search for at least one active subscription for the given email address.
  $email = $form_state
    ->getValue('email');
  $subscription_result = $this->subscriptionStorage
    ->getQuery()
    ->condition('status', SubscriptionInterface::ACTIVE)
    ->condition('email', $email)
    ->condition('uid', 0)
    ->range(0, 1)
    ->execute();
  if (count($subscription_result)) {

    /** @var \Drupal\mailing_list\SubscriptionInterface $subscription */
    $subscription = $this->subscriptionStorage
      ->load(array_pop($subscription_result));
    $manage_url = Url::fromRoute('mailing_list.access_subscription', [
      'sid' => $subscription
        ->id(),
      'hash' => $subscription
        ->getAccessHash(),
      'rel' => 'manage',
    ]);
    if ($manage_url
      ->access()) {
      $this->mailManager
        ->mail('mailing_list', 'anonymous_subscription_access', $email, $subscription
        ->language(), [
        'manage_url' => $manage_url
          ->setAbsolute()
          ->toString(),
      ]);
    }
  }

  // Returns the same message to prevent subscribers email disclosure.
  drupal_set_message($this
    ->t('Your request has been successfully processed. You will receive a message with access instructions only if at least one active subscription was found for your email. If you do not receive any messages in short, you probably do not have any active subscription on this site.'));
  $form_state
    ->setRedirectUrl(Url::fromRoute('<front>'));
}