You are here

public function SubscriptionsFormBase::submitUpdate in Simplenews 8.2

Same name and namespace in other branches
  1. 8 src/Form/SubscriptionsFormBase.php \Drupal\simplenews\Form\SubscriptionsFormBase::submitUpdate()

Submit callback that (un)subscribes to newsletters based on selection.

Parameters

array $form: The form structure.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

File

src/Form/SubscriptionsFormBase.php, line 306

Class

SubscriptionsFormBase
Entity form for Subscriber with common routines.

Namespace

Drupal\simplenews\Form

Code

public function submitUpdate(array $form, FormStateInterface $form_state) {

  // We first subscribe, then unsubscribe. This prevents deletion of
  // subscriptions when unsubscribed from the newsletter.

  /** @var \Drupal\simplenews\Subscription\SubscriptionManagerInterface $subscription_manager */
  $subscription_manager = \Drupal::service('simplenews.subscription_manager');
  foreach ($this
    ->extractNewsletterIds($form_state, TRUE) as $newsletter_id) {
    $subscription_manager
      ->subscribe($this->entity
      ->getMail(), $newsletter_id, FALSE, 'website');
  }
  if (!$this->entity
    ->isNew()) {
    foreach ($this
      ->extractNewsletterIds($form_state, FALSE) as $newsletter_id) {
      $subscription_manager
        ->unsubscribe($this->entity
        ->getMail(), $newsletter_id, FALSE, 'website');
    }
  }
  $this
    ->messenger()
    ->addMessage($this
    ->getSubmitMessage($form_state, static::SUBMIT_UPDATE, FALSE));
}