You are here

public function CategoryForm::save in Mass Contact 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/CategoryForm.php, line 116

Class

CategoryForm
Class CategoryForm.

Namespace

Drupal\mass_contact\Form

Code

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

  /** @var \Drupal\mass_contact\Entity\MassContactCategoryInterface $mass_contact_category */
  $mass_contact_category = $this->entity;
  $recipients = $mass_contact_category
    ->getRecipients();
  foreach ($recipients as $plugin_id => $definition) {
    $recipients[$plugin_id]['categories'] = array_values(array_filter($definition['categories']));
  }
  $mass_contact_category
    ->setRecipients($recipients);
  $status = $mass_contact_category
    ->save();
  switch ($status) {
    case SAVED_NEW:
      $this
        ->messenger()
        ->addStatus($this
        ->t('Created the %label category.', [
        '%label' => $mass_contact_category
          ->label(),
      ]));
      break;
    default:
      $this
        ->messenger()
        ->addStatus($this
        ->t('Saved the %label category.', [
        '%label' => $mass_contact_category
          ->label(),
      ]));
  }

  // TODO: Drupal Rector Notice: Please delete the following comment after you've made any necessary changes.
  // Please confirm that `$mass_contact_category` is an instance of `Drupal\Core\Entity\EntityInterface`. Only the method name and not the class name was checked for this replacement, so this may be a false positive.
  $form_state
    ->setRedirectUrl($mass_contact_category
    ->toUrl('collection'));
}