public OverviewTerms::submitForm(array &$form, FormStateInterface $form_state)
Form submission handler.
Rather than using a textfield or weight field, this form depends entirely upon the order of form elements on the page to determine new weights.
Because there might be hundreds or thousands of taxonomy terms that need to be ordered, terms are weighted from 0 to the number of terms in the vocabulary, rather than the standard -10 to 10 scale. Numbers are sorted lowest to highest, but are not necessarily sequential. Numbers may be skipped when a term has children so that reordering is minimal when a child is added or removed from a term.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides FormInterface::submitForm
File
- core/modules/taxonomy/src/Form/OverviewTerms.php, line 379
Class
- OverviewTerms
- Provides terms overview form for a taxonomy vocabulary.
Namespace
Drupal\taxonomy\Form
Code
public function submitForm(array &$form, FormStateInterface $form_state) { // Sort term order based on weight. uasort($form_state->getValue('terms'), array('Drupal\Component\Utility\SortArray', 'sortByWeightElement')); $vocabulary = $form_state->get(['taxonomy', 'vocabulary']); // Update the current hierarchy type as we go. $hierarchy = VocabularyInterface::HIERARCHY_DISABLED; $changed_terms = array(); $tree = $this->storageController->loadTree($vocabulary->id(), 0, NULL, TRUE); if (empty($tree)) { return; } // Build a list of all terms that need to be updated on previous pages. $weight = 0; $term = $tree[0]; while ($term->id() != $form['#first_tid']) { if ($term->parents[0] == 0 && $term->getWeight() != $weight) { $term->setWeight($weight); $changed_terms[$term->id()] = $term; } $weight++; $hierarchy = $term->parents[0] != 0 ? VocabularyInterface::HIERARCHY_SINGLE : $hierarchy; $term = $tree[$weight]; } // Renumber the current page weights and assign any new parents. $level_weights = array(); foreach ($form_state->getValue('terms') as $tid => $values) { if (isset($form['terms'][$tid]['#term'])) { $term = $form['terms'][$tid]['#term']; // Give terms at the root level a weight in sequence with terms on previous pages. if ($values['term']['parent'] == 0 && $term->getWeight() != $weight) { $term->setWeight($weight); $changed_terms[$term->id()] = $term; } // Terms not at the root level can safely start from 0 because they're all on this page. elseif ($values['term']['parent'] > 0) { $level_weights[$values['term']['parent']] = isset($level_weights[$values['term']['parent']]) ? $level_weights[$values['term']['parent']] + 1 : 0; if ($level_weights[$values['term']['parent']] != $term->getWeight()) { $term->setWeight($level_weights[$values['term']['parent']]); $changed_terms[$term->id()] = $term; } } // Update any changed parents. if ($values['term']['parent'] != $term->parents[0]) { $term->parent->target_id = $values['term']['parent']; $changed_terms[$term->id()] = $term; } $hierarchy = $term->parents[0] != 0 ? VocabularyInterface::HIERARCHY_SINGLE : $hierarchy; $weight++; } } // Build a list of all terms that need to be updated on following pages. for ($weight; $weight < count($tree); $weight++) { $term = $tree[$weight]; if ($term->parents[0] == 0 && $term->getWeight() != $weight) { $term->parent->target_id = $term->parents[0]; $term->setWeight($weight); $changed_terms[$term->id()] = $term; } $hierarchy = $term->parents[0] != 0 ? VocabularyInterface::HIERARCHY_SINGLE : $hierarchy; } // Save all updated terms. foreach ($changed_terms as $term) { $term->save(); } // Update the vocabulary hierarchy to flat or single hierarchy. if ($vocabulary->getHierarchy() != $hierarchy) { $vocabulary->setHierarchy($hierarchy); $vocabulary->save(); } drupal_set_message($this->t('The configuration options have been saved.')); }
Please login to continue.