function VocabularyForm::save

Same name and namespace in other branches
  1. 9 core/modules/taxonomy/src/VocabularyForm.php \Drupal\taxonomy\VocabularyForm::save()
  2. 8.9.x core/modules/taxonomy/src/VocabularyForm.php \Drupal\taxonomy\VocabularyForm::save()
  3. 10 core/modules/taxonomy/src/VocabularyForm.php \Drupal\taxonomy\VocabularyForm::save()

Overrides EntityForm::save

File

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

Class

VocabularyForm
Base form for vocabulary edit forms.

Namespace

Drupal\taxonomy

Code

public function save(array $form, FormStateInterface $form_state) {
    $vocabulary = $this->entity;
    $vocabulary->setNewRevision($form_state->getValue([
        'revision',
    ]));
    // 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());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.