You are here

public function TermForm::save in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 126
Contains \Drupal\taxonomy\TermForm.

Class

TermForm
Base for controller for taxonomy term edit forms.

Namespace

Drupal\taxonomy

Code

public function save(array $form, FormStateInterface $form_state) {
  $term = $this->entity;
  $result = $term
    ->save();
  $link = $term
    ->link($this
    ->t('Edit'), 'edit-form');
  switch ($result) {
    case SAVED_NEW:
      drupal_set_message($this
        ->t('Created new term %term.', array(
        '%term' => $term
          ->getName(),
      )));
      $this
        ->logger('taxonomy')
        ->notice('Created new term %term.', array(
        '%term' => $term
          ->getName(),
        'link' => $link,
      ));
      break;
    case SAVED_UPDATED:
      drupal_set_message($this
        ->t('Updated term %term.', array(
        '%term' => $term
          ->getName(),
      )));
      $this
        ->logger('taxonomy')
        ->notice('Updated term %term.', array(
        '%term' => $term
          ->getName(),
        'link' => $link,
      ));
      break;
  }
  $current_parent_count = count($form_state
    ->getValue('parent'));
  $previous_parent_count = count($form_state
    ->get([
    'taxonomy',
    'parent',
  ]));

  // Root doesn't count if it's the only parent.
  if ($current_parent_count == 1 && $form_state
    ->hasValue(array(
    'parent',
    0,
  ))) {
    $current_parent_count = 0;
    $form_state
      ->setValue('parent', array());
  }

  // If the number of parents has been reduced to one or none, do a check on the
  // parents of every term in the vocabulary value.
  $vocabulary = $form_state
    ->get([
    'taxonomy',
    'vocabulary',
  ]);
  if ($current_parent_count < $previous_parent_count && $current_parent_count < 2) {
    taxonomy_check_vocabulary_hierarchy($vocabulary, $form_state
      ->getValues());
  }
  elseif ($current_parent_count > $previous_parent_count && $vocabulary
    ->getHierarchy() != TAXONOMY_HIERARCHY_MULTIPLE) {
    $vocabulary
      ->setHierarchy($current_parent_count == 1 ? TAXONOMY_HIERARCHY_SINGLE : TAXONOMY_HIERARCHY_MULTIPLE);
    $vocabulary
      ->save();
  }
  $form_state
    ->setValue('tid', $term
    ->id());
  $form_state
    ->set('tid', $term
    ->id());
}