You are here

public function SubscriptionForm::save in Mailing List 8

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

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

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

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

src/Form/SubscriptionForm.php, line 99

Class

SubscriptionForm
Form controller for mailing list subscription form.

Namespace

Drupal\mailing_list\Form

Code

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

  /** @var \Drupal\mailing_list\SubscriptionInterface $entity */
  $entity = $this->entity;

  /** @var \Drupal\mailing_list\MailingListInterface $list */
  $list = $entity
    ->getList();
  $status = parent::save($form, $form_state);
  switch ($status) {
    case SAVED_NEW:
      if ($message = $list
        ->getOnSubscriptionMessage()) {
        drupal_set_message($message);
      }
      elseif ($this
        ->currentUser()
        ->id() == $entity
        ->getOwnerId()) {
        drupal_set_message($this
          ->t('Your subscription %label to the %list mailing list has been processed. Check your email for further details.', [
          '%label' => $entity
            ->label(),
          '%list' => $list
            ->label(),
        ]));
      }
      else {
        drupal_set_message($this
          ->t('Subscription %label to the %list mailing list has been processed.', [
          '%label' => $entity
            ->label(),
          '%list' => $list
            ->label(),
        ]));
      }
      break;
    default:
      drupal_set_message($this
        ->t('Subscription %label to the %list mailing list has been updated.', [
        '%label' => $entity
          ->label(),
        '%list' => $list
          ->label(),
      ]));
  }

  // Set form destination.
  $url = $entity
    ->toUrl($this->destination ?: 'form-destination');
  $form_state
    ->setRedirect($url
    ->getRouteName(), $url
    ->getRouteParameters());
}