You are here

public function EasyEmailForm::save in Easy Email 8

Same name and namespace in other branches
  1. 2.0.x src/Form/EasyEmailForm.php \Drupal\easy_email\Form\EasyEmailForm::save()

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/EasyEmailForm.php, line 398

Class

EasyEmailForm
Form controller for Email edit forms.

Namespace

Drupal\easy_email\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $this
    ->setValuesFromFormState($form, $form_state);

  /** @var \Drupal\easy_email\Entity\EasyEmailInterface $entity */
  $entity = $this->entity;

  // Save as a new revision if requested to do so.
  if (!$form_state
    ->isValueEmpty('new_revision') && $form_state
    ->getValue('new_revision') != FALSE) {
    $entity
      ->setNewRevision();

    // If a new revision is created, save the current user as revision author.
    $entity
      ->setRevisionCreationTime(\Drupal::time()
      ->getRequestTime());
    $entity
      ->setRevisionUserId(\Drupal::currentUser()
      ->id());
  }
  else {
    $entity
      ->setNewRevision(FALSE);
  }
  $status = parent::save($form, $form_state);
  switch ($status) {
    case SAVED_NEW:
      \Drupal::messenger()
        ->addStatus($this
        ->t('Created new email.'));
      break;
    default:
      \Drupal::messenger()
        ->addStatus($this
        ->t('Saved email.'));
  }
  if (!empty($form_state
    ->getValue('send'))) {
    $status = \Drupal::service('easy_email.handler')
      ->sendEmail($entity);
    if ($status) {
      \Drupal::messenger()
        ->addStatus($this
        ->t('Email sent.'));
    }
  }
  $form_state
    ->setRedirect('entity.easy_email.canonical', [
    'easy_email' => $entity
      ->id(),
  ]);
}