You are here

public function ConfirmMultiForm::submitForm in Simplenews 8

Same name and namespace in other branches
  1. 8.2 src/Form/ConfirmMultiForm.php \Drupal\simplenews\Form\ConfirmMultiForm::submitForm()
  2. 3.x src/Form/ConfirmMultiForm.php \Drupal\simplenews\Form\ConfirmMultiForm::submitForm()

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/ConfirmMultiForm.php, line 77

Class

ConfirmMultiForm
Implements a multi confirmation form for simplenews subscriptions.

Namespace

Drupal\simplenews\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $subscriber = $form_state
    ->getValue('subscriber');
  foreach ($subscriber
    ->getChanges() as $newsletter_id => $action) {
    if ($action == 'subscribe') {
      if (!$subscriber
        ->isSubscribed($newsletter_id)) {

        // Subscribe the user if not already subscribed.
        $subscriber
          ->subscribe($newsletter_id);
      }
    }
    elseif ($action == 'unsubscribe') {
      if ($subscriber
        ->isSubscribed($newsletter_id)) {

        // Subscribe the user if not already subscribed.
        $subscriber
          ->unsubscribe($newsletter_id);
      }
    }
  }

  // Clear changes.
  $subscriber
    ->setChanges(array());
  $subscriber
    ->save();
  $this
    ->messenger()
    ->addMessage(t('Subscription changes confirmed for %user.', array(
    '%user' => $subscriber
      ->getMail(),
  )));
  $form_state
    ->setRedirect('<front>');
}