You are here

public function TermForm::save in Drupal 8

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

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 153

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,
      ]);
      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());
}