function TermForm::save

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

Overrides EntityForm::save

1 method overrides TermForm::save()
ForumForm::save in core/modules/forum/src/Form/ForumForm.php
Form submission handler for the 'save' action.

File

core/modules/taxonomy/src/TermForm.php, line 188

Class

TermForm
Base for handler for taxonomy term edit forms.

Namespace

Drupal\taxonomy

Code

public function save(array $form, FormStateInterface $form_state) {
    $term = $this->entity;
    $result = $term->save();
    $edit_link = $term->toLink($this->t('Edit'), 'edit-form')
        ->toString();
    $view_link = $term->toLink()
        ->toString();
    switch ($result) {
        case SAVED_NEW:
            $this->messenger()
                ->addStatus($this->t('Created new term %term.', [
                '%term' => $view_link,
            ]));
            $this->logger('taxonomy')
                ->notice('Created new term %term.', [
                '%term' => $term->getName(),
                'link' => $edit_link,
            ]);
            break;
        case SAVED_UPDATED:
            $this->messenger()
                ->addStatus($this->t('Updated term %term.', [
                '%term' => $view_link,
            ]));
            $this->logger('taxonomy')
                ->notice('Updated term %term.', [
                '%term' => $term->getName(),
                'link' => $edit_link,
            ]);
            $form_state->setRedirect('entity.taxonomy_term.canonical', [
                'taxonomy_term' => $term->id(),
            ]);
            break;
    }
    $current_parent_count = count($form_state->getValue('parent'));
    // Root doesn't count if it's the only parent.
    if ($current_parent_count == 1 && $form_state->hasValue([
        'parent',
        0,
    ])) {
        $form_state->setValue('parent', []);
    }
    $form_state->setValue('tid', $term->id());
    $form_state->set('tid', $term->id());
}

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