You are here

public function VocabularyForm::save in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/taxonomy/src/VocabularyForm.php \Drupal\taxonomy\VocabularyForm::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

core/modules/taxonomy/src/VocabularyForm.php, line 118

Class

VocabularyForm
Base form for vocabulary edit forms.

Namespace

Drupal\taxonomy

Code

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

  // Prevent leading and trailing spaces in vocabulary names.
  $vocabulary
    ->set('name', trim($vocabulary
    ->label()));
  $status = $vocabulary
    ->save();
  $edit_link = $this->entity
    ->toLink($this
    ->t('Edit'), 'edit-form')
    ->toString();
  switch ($status) {
    case SAVED_NEW:
      $this
        ->messenger()
        ->addStatus($this
        ->t('Created new vocabulary %name.', [
        '%name' => $vocabulary
          ->label(),
      ]));
      $this
        ->logger('taxonomy')
        ->notice('Created new vocabulary %name.', [
        '%name' => $vocabulary
          ->label(),
        'link' => $edit_link,
      ]);
      $form_state
        ->setRedirectUrl($vocabulary
        ->toUrl('overview-form'));
      break;
    case SAVED_UPDATED:
      $this
        ->messenger()
        ->addStatus($this
        ->t('Updated vocabulary %name.', [
        '%name' => $vocabulary
          ->label(),
      ]));
      $this
        ->logger('taxonomy')
        ->notice('Updated vocabulary %name.', [
        '%name' => $vocabulary
          ->label(),
        'link' => $edit_link,
      ]);
      $form_state
        ->setRedirectUrl($vocabulary
        ->toUrl('collection'));
      break;
  }
  $form_state
    ->setValue('vid', $vocabulary
    ->id());
  $form_state
    ->set('vid', $vocabulary
    ->id());
}