You are here

public function VocabularyForm::save in Drupal 10

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