TermForm::save

public TermForm::save(array $form, FormStateInterface $form_state)

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

File

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

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->link($this->t('Edit'), 'edit-form');
  $view_link = $term->link($term->getName());
  switch ($result) {
    case SAVED_NEW:
      drupal_set_message($this->t('Created new term %term.', array('%term' => $view_link)));
      $this->logger('taxonomy')->notice('Created new term %term.', array('%term' => $term->getName(), 'link' => $edit_link));
      break;
    case SAVED_UPDATED:
      drupal_set_message($this->t('Updated term %term.', array('%term' => $view_link)));
      $this->logger('taxonomy')->notice('Updated term %term.', array('%term' => $term->getName(), 'link' => $edit_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());
  }
  // If we've increased the number of parents and this is a single or flat
  // hierarchy, update the vocabulary immediately.
  elseif ($current_parent_count > $previous_parent_count && $vocabulary->getHierarchy() != VocabularyInterface::HIERARCHY_MULTIPLE) {
    $vocabulary->setHierarchy($current_parent_count == 1 ? VocabularyInterface::HIERARCHY_SINGLE : VocabularyInterface::HIERARCHY_MULTIPLE);
    $vocabulary->save();
  }

  $form_state->setValue('tid', $term->id());
  $form_state->set('tid', $term->id());
}
doc_Drupal
2016-10-29 09:47:12
Comments
Leave a Comment

Please login to continue.