You are here

public function EmailOverrideForm::save in Easy Email 8

Same name and namespace in other branches
  1. 2.0.x modules/easy_email_override/src/Form/EmailOverrideForm.php \Drupal\easy_email_override\Form\EmailOverrideForm::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

modules/easy_email_override/src/Form/EmailOverrideForm.php, line 234

Class

EmailOverrideForm
Class EmailOverrideForm.

Namespace

Drupal\easy_email_override\Form

Code

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

  /** @var \Drupal\easy_email_override\Entity\EmailOverrideInterface $easy_email_override */
  $easy_email_override = $this->entity;
  $is_new = $easy_email_override
    ->isNew();
  $definition = $this->emailManager
    ->getDefinition($form_state
    ->getValue('email_id'));
  $easy_email_override
    ->setModule($definition['module'])
    ->setKey($definition['key'])
    ->setEasyEmailType($form_state
    ->getValue('easy_email_type'));
  if (!$is_new) {
    $param_mapping = [];
    foreach ($form_state
      ->getValue('mappings') as $source_id => $dest_id) {
      $param_mapping[] = [
        'source' => $source_id,
        'destination' => $dest_id,
      ];
    }
    $easy_email_override
      ->setParamMap($param_mapping);
  }
  $status = $easy_email_override
    ->save();
  if ($is_new) {
    $form_state
      ->setRedirectUrl($easy_email_override
      ->toUrl('edit-form'));
  }
  else {
    $form_state
      ->setRedirectUrl($easy_email_override
      ->toUrl('collection'));
  }
  switch ($status) {
    case SAVED_NEW:
      \Drupal::messenger()
        ->addStatus($this
        ->t('Created the %label email override. Please configure any parameter mappings necessary below.', [
        '%label' => $easy_email_override
          ->label(),
      ]));
      break;
    default:
      \Drupal::messenger()
        ->addStatus($this
        ->t('Saved the %label email override.', [
        '%label' => $easy_email_override
          ->label(),
      ]));
  }
}