annotate core/modules/taxonomy/src/Form/OverviewTerms.php @ 16:c2387f117808

Routine composer update
author Chris Cannam
date Tue, 10 Jul 2018 15:07:59 +0100
parents 1fec387a4317
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\taxonomy\Form;
Chris@0 4
Chris@14 5 use Drupal\Core\Access\AccessResult;
Chris@0 6 use Drupal\Core\Entity\EntityManagerInterface;
Chris@0 7 use Drupal\Core\Form\FormBase;
Chris@0 8 use Drupal\Core\Extension\ModuleHandlerInterface;
Chris@0 9 use Drupal\Core\Form\FormStateInterface;
Chris@14 10 use Drupal\Core\Render\RendererInterface;
Chris@14 11 use Drupal\Core\Url;
Chris@0 12 use Drupal\taxonomy\VocabularyInterface;
Chris@0 13 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Provides terms overview form for a taxonomy vocabulary.
Chris@14 17 *
Chris@14 18 * @internal
Chris@0 19 */
Chris@0 20 class OverviewTerms extends FormBase {
Chris@0 21
Chris@0 22 /**
Chris@0 23 * The module handler service.
Chris@0 24 *
Chris@0 25 * @var \Drupal\Core\Extension\ModuleHandlerInterface
Chris@0 26 */
Chris@0 27 protected $moduleHandler;
Chris@0 28
Chris@0 29 /**
Chris@0 30 * The entity manager.
Chris@0 31 *
Chris@0 32 * @var \Drupal\Core\Entity\EntityManagerInterface
Chris@0 33 */
Chris@0 34 protected $entityManager;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * The term storage handler.
Chris@0 38 *
Chris@0 39 * @var \Drupal\taxonomy\TermStorageInterface
Chris@0 40 */
Chris@0 41 protected $storageController;
Chris@0 42
Chris@0 43 /**
Chris@14 44 * The term list builder.
Chris@14 45 *
Chris@14 46 * @var \Drupal\Core\Entity\EntityListBuilderInterface
Chris@14 47 */
Chris@14 48 protected $termListBuilder;
Chris@14 49
Chris@14 50 /**
Chris@14 51 * The renderer service.
Chris@14 52 *
Chris@14 53 * @var \Drupal\Core\Render\RendererInterface
Chris@14 54 */
Chris@14 55 protected $renderer;
Chris@14 56
Chris@14 57 /**
Chris@0 58 * Constructs an OverviewTerms object.
Chris@0 59 *
Chris@0 60 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
Chris@0 61 * The module handler service.
Chris@0 62 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Chris@0 63 * The entity manager service.
Chris@14 64 * @param \Drupal\Core\Render\RendererInterface $renderer
Chris@14 65 * The renderer service.
Chris@0 66 */
Chris@14 67 public function __construct(ModuleHandlerInterface $module_handler, EntityManagerInterface $entity_manager, RendererInterface $renderer = NULL) {
Chris@0 68 $this->moduleHandler = $module_handler;
Chris@0 69 $this->entityManager = $entity_manager;
Chris@0 70 $this->storageController = $entity_manager->getStorage('taxonomy_term');
Chris@14 71 $this->termListBuilder = $entity_manager->getListBuilder('taxonomy_term');
Chris@14 72 $this->renderer = $renderer ?: \Drupal::service('renderer');
Chris@0 73 }
Chris@0 74
Chris@0 75 /**
Chris@0 76 * {@inheritdoc}
Chris@0 77 */
Chris@0 78 public static function create(ContainerInterface $container) {
Chris@0 79 return new static(
Chris@0 80 $container->get('module_handler'),
Chris@14 81 $container->get('entity.manager'),
Chris@14 82 $container->get('renderer')
Chris@0 83 );
Chris@0 84 }
Chris@0 85
Chris@0 86 /**
Chris@0 87 * {@inheritdoc}
Chris@0 88 */
Chris@0 89 public function getFormId() {
Chris@0 90 return 'taxonomy_overview_terms';
Chris@0 91 }
Chris@0 92
Chris@0 93 /**
Chris@0 94 * Form constructor.
Chris@0 95 *
Chris@0 96 * Display a tree of all the terms in a vocabulary, with options to edit
Chris@0 97 * each one. The form is made drag and drop by the theme function.
Chris@0 98 *
Chris@0 99 * @param array $form
Chris@0 100 * An associative array containing the structure of the form.
Chris@0 101 * @param \Drupal\Core\Form\FormStateInterface $form_state
Chris@0 102 * The current state of the form.
Chris@0 103 * @param \Drupal\taxonomy\VocabularyInterface $taxonomy_vocabulary
Chris@0 104 * The vocabulary to display the overview form for.
Chris@0 105 *
Chris@0 106 * @return array
Chris@0 107 * The form structure.
Chris@0 108 */
Chris@0 109 public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL) {
Chris@0 110 // @todo Remove global variables when https://www.drupal.org/node/2044435 is
Chris@0 111 // in.
Chris@0 112 global $pager_page_array, $pager_total, $pager_total_items;
Chris@0 113
Chris@0 114 $form_state->set(['taxonomy', 'vocabulary'], $taxonomy_vocabulary);
Chris@0 115 $parent_fields = FALSE;
Chris@0 116
Chris@0 117 $page = $this->getRequest()->query->get('page') ?: 0;
Chris@0 118 // Number of terms per page.
Chris@0 119 $page_increment = $this->config('taxonomy.settings')->get('terms_per_page_admin');
Chris@0 120 // Elements shown on this page.
Chris@0 121 $page_entries = 0;
Chris@0 122 // Elements at the root level before this page.
Chris@0 123 $before_entries = 0;
Chris@0 124 // Elements at the root level after this page.
Chris@0 125 $after_entries = 0;
Chris@0 126 // Elements at the root level on this page.
Chris@0 127 $root_entries = 0;
Chris@0 128
Chris@0 129 // Terms from previous and next pages are shown if the term tree would have
Chris@0 130 // been cut in the middle. Keep track of how many extra terms we show on
Chris@0 131 // each page of terms.
Chris@0 132 $back_step = NULL;
Chris@0 133 $forward_step = 0;
Chris@0 134
Chris@0 135 // An array of the terms to be displayed on this page.
Chris@0 136 $current_page = [];
Chris@0 137
Chris@0 138 $delta = 0;
Chris@0 139 $term_deltas = [];
Chris@0 140 $tree = $this->storageController->loadTree($taxonomy_vocabulary->id(), 0, NULL, TRUE);
Chris@0 141 $tree_index = 0;
Chris@0 142 do {
Chris@0 143 // In case this tree is completely empty.
Chris@0 144 if (empty($tree[$tree_index])) {
Chris@0 145 break;
Chris@0 146 }
Chris@0 147 $delta++;
Chris@0 148 // Count entries before the current page.
Chris@0 149 if ($page && ($page * $page_increment) > $before_entries && !isset($back_step)) {
Chris@0 150 $before_entries++;
Chris@0 151 continue;
Chris@0 152 }
Chris@0 153 // Count entries after the current page.
Chris@0 154 elseif ($page_entries > $page_increment && isset($complete_tree)) {
Chris@0 155 $after_entries++;
Chris@0 156 continue;
Chris@0 157 }
Chris@0 158
Chris@0 159 // Do not let a term start the page that is not at the root.
Chris@0 160 $term = $tree[$tree_index];
Chris@0 161 if (isset($term->depth) && ($term->depth > 0) && !isset($back_step)) {
Chris@0 162 $back_step = 0;
Chris@0 163 while ($pterm = $tree[--$tree_index]) {
Chris@0 164 $before_entries--;
Chris@0 165 $back_step++;
Chris@0 166 if ($pterm->depth == 0) {
Chris@0 167 $tree_index--;
Chris@0 168 // Jump back to the start of the root level parent.
Chris@0 169 continue 2;
Chris@0 170 }
Chris@0 171 }
Chris@0 172 }
Chris@0 173 $back_step = isset($back_step) ? $back_step : 0;
Chris@0 174
Chris@0 175 // Continue rendering the tree until we reach the a new root item.
Chris@0 176 if ($page_entries >= $page_increment + $back_step + 1 && $term->depth == 0 && $root_entries > 1) {
Chris@0 177 $complete_tree = TRUE;
Chris@0 178 // This new item at the root level is the first item on the next page.
Chris@0 179 $after_entries++;
Chris@0 180 continue;
Chris@0 181 }
Chris@0 182 if ($page_entries >= $page_increment + $back_step) {
Chris@0 183 $forward_step++;
Chris@0 184 }
Chris@0 185
Chris@0 186 // Finally, if we've gotten down this far, we're rendering a term on this
Chris@0 187 // page.
Chris@0 188 $page_entries++;
Chris@0 189 $term_deltas[$term->id()] = isset($term_deltas[$term->id()]) ? $term_deltas[$term->id()] + 1 : 0;
Chris@0 190 $key = 'tid:' . $term->id() . ':' . $term_deltas[$term->id()];
Chris@0 191
Chris@0 192 // Keep track of the first term displayed on this page.
Chris@0 193 if ($page_entries == 1) {
Chris@0 194 $form['#first_tid'] = $term->id();
Chris@0 195 }
Chris@0 196 // Keep a variable to make sure at least 2 root elements are displayed.
Chris@0 197 if ($term->parents[0] == 0) {
Chris@0 198 $root_entries++;
Chris@0 199 }
Chris@0 200 $current_page[$key] = $term;
Chris@0 201 } while (isset($tree[++$tree_index]));
Chris@0 202
Chris@0 203 // Because we didn't use a pager query, set the necessary pager variables.
Chris@0 204 $total_entries = $before_entries + $page_entries + $after_entries;
Chris@0 205 $pager_total_items[0] = $total_entries;
Chris@0 206 $pager_page_array[0] = $page;
Chris@0 207 $pager_total[0] = ceil($total_entries / $page_increment);
Chris@0 208
Chris@0 209 // If this form was already submitted once, it's probably hit a validation
Chris@0 210 // error. Ensure the form is rebuilt in the same order as the user
Chris@0 211 // submitted.
Chris@0 212 $user_input = $form_state->getUserInput();
Chris@0 213 if (!empty($user_input)) {
Chris@0 214 // Get the POST order.
Chris@0 215 $order = array_flip(array_keys($user_input['terms']));
Chris@0 216 // Update our form with the new order.
Chris@0 217 $current_page = array_merge($order, $current_page);
Chris@0 218 foreach ($current_page as $key => $term) {
Chris@0 219 // Verify this is a term for the current page and set at the current
Chris@0 220 // depth.
Chris@0 221 if (is_array($user_input['terms'][$key]) && is_numeric($user_input['terms'][$key]['term']['tid'])) {
Chris@0 222 $current_page[$key]->depth = $user_input['terms'][$key]['term']['depth'];
Chris@0 223 }
Chris@0 224 else {
Chris@0 225 unset($current_page[$key]);
Chris@0 226 }
Chris@0 227 }
Chris@0 228 }
Chris@0 229
Chris@0 230 $errors = $form_state->getErrors();
Chris@0 231 $row_position = 0;
Chris@0 232 // Build the actual form.
Chris@14 233 $access_control_handler = $this->entityManager->getAccessControlHandler('taxonomy_term');
Chris@14 234 $create_access = $access_control_handler->createAccess($taxonomy_vocabulary->id(), NULL, [], TRUE);
Chris@14 235 if ($create_access->isAllowed()) {
Chris@14 236 $empty = $this->t('No terms available. <a href=":link">Add term</a>.', [':link' => Url::fromRoute('entity.taxonomy_term.add_form', ['taxonomy_vocabulary' => $taxonomy_vocabulary->id()])->toString()]);
Chris@14 237 }
Chris@14 238 else {
Chris@14 239 $empty = $this->t('No terms available.');
Chris@14 240 }
Chris@0 241 $form['terms'] = [
Chris@0 242 '#type' => 'table',
Chris@14 243 '#empty' => $empty,
Chris@16 244 '#header' => [
Chris@16 245 'term' => $this->t('Name'),
Chris@16 246 'operations' => $this->t('Operations'),
Chris@16 247 'weight' => $this->t('Weight'),
Chris@16 248 ],
Chris@0 249 '#attributes' => [
Chris@0 250 'id' => 'taxonomy',
Chris@0 251 ],
Chris@0 252 ];
Chris@14 253 $this->renderer->addCacheableDependency($form['terms'], $create_access);
Chris@14 254
Chris@14 255 // Only allow access to changing weights if the user has update access for
Chris@14 256 // all terms.
Chris@14 257 $change_weight_access = AccessResult::allowed();
Chris@0 258 foreach ($current_page as $key => $term) {
Chris@16 259 $form['terms'][$key] = [
Chris@16 260 'term' => [],
Chris@16 261 'operations' => [],
Chris@16 262 'weight' => [],
Chris@16 263 ];
Chris@0 264 /** @var $term \Drupal\Core\Entity\EntityInterface */
Chris@0 265 $term = $this->entityManager->getTranslationFromContext($term);
Chris@0 266 $form['terms'][$key]['#term'] = $term;
Chris@0 267 $indentation = [];
Chris@0 268 if (isset($term->depth) && $term->depth > 0) {
Chris@0 269 $indentation = [
Chris@0 270 '#theme' => 'indentation',
Chris@0 271 '#size' => $term->depth,
Chris@0 272 ];
Chris@0 273 }
Chris@0 274 $form['terms'][$key]['term'] = [
Chris@0 275 '#prefix' => !empty($indentation) ? \Drupal::service('renderer')->render($indentation) : '',
Chris@0 276 '#type' => 'link',
Chris@0 277 '#title' => $term->getName(),
Chris@0 278 '#url' => $term->urlInfo(),
Chris@0 279 ];
Chris@0 280 if ($taxonomy_vocabulary->getHierarchy() != VocabularyInterface::HIERARCHY_MULTIPLE && count($tree) > 1) {
Chris@0 281 $parent_fields = TRUE;
Chris@0 282 $form['terms'][$key]['term']['tid'] = [
Chris@0 283 '#type' => 'hidden',
Chris@0 284 '#value' => $term->id(),
Chris@0 285 '#attributes' => [
Chris@0 286 'class' => ['term-id'],
Chris@0 287 ],
Chris@0 288 ];
Chris@0 289 $form['terms'][$key]['term']['parent'] = [
Chris@0 290 '#type' => 'hidden',
Chris@0 291 // Yes, default_value on a hidden. It needs to be changeable by the
Chris@0 292 // javascript.
Chris@0 293 '#default_value' => $term->parents[0],
Chris@0 294 '#attributes' => [
Chris@0 295 'class' => ['term-parent'],
Chris@0 296 ],
Chris@0 297 ];
Chris@0 298 $form['terms'][$key]['term']['depth'] = [
Chris@0 299 '#type' => 'hidden',
Chris@0 300 // Same as above, the depth is modified by javascript, so it's a
Chris@0 301 // default_value.
Chris@0 302 '#default_value' => $term->depth,
Chris@0 303 '#attributes' => [
Chris@0 304 'class' => ['term-depth'],
Chris@0 305 ],
Chris@0 306 ];
Chris@0 307 }
Chris@14 308 $update_access = $term->access('update', NULL, TRUE);
Chris@14 309 $change_weight_access = $change_weight_access->andIf($update_access);
Chris@14 310
Chris@14 311 if ($update_access->isAllowed()) {
Chris@14 312 $form['terms'][$key]['weight'] = [
Chris@14 313 '#type' => 'weight',
Chris@14 314 '#delta' => $delta,
Chris@14 315 '#title' => $this->t('Weight for added term'),
Chris@14 316 '#title_display' => 'invisible',
Chris@14 317 '#default_value' => $term->getWeight(),
Chris@14 318 '#attributes' => ['class' => ['term-weight']],
Chris@0 319 ];
Chris@0 320 }
Chris@14 321
Chris@14 322 if ($operations = $this->termListBuilder->getOperations($term)) {
Chris@14 323 $form['terms'][$key]['operations'] = [
Chris@14 324 '#type' => 'operations',
Chris@14 325 '#links' => $operations,
Chris@14 326 ];
Chris@14 327 }
Chris@0 328
Chris@0 329 $form['terms'][$key]['#attributes']['class'] = [];
Chris@0 330 if ($parent_fields) {
Chris@0 331 $form['terms'][$key]['#attributes']['class'][] = 'draggable';
Chris@0 332 }
Chris@0 333
Chris@0 334 // Add classes that mark which terms belong to previous and next pages.
Chris@0 335 if ($row_position < $back_step || $row_position >= $page_entries - $forward_step) {
Chris@0 336 $form['terms'][$key]['#attributes']['class'][] = 'taxonomy-term-preview';
Chris@0 337 }
Chris@0 338
Chris@0 339 if ($row_position !== 0 && $row_position !== count($tree) - 1) {
Chris@0 340 if ($row_position == $back_step - 1 || $row_position == $page_entries - $forward_step - 1) {
Chris@0 341 $form['terms'][$key]['#attributes']['class'][] = 'taxonomy-term-divider-top';
Chris@0 342 }
Chris@0 343 elseif ($row_position == $back_step || $row_position == $page_entries - $forward_step) {
Chris@0 344 $form['terms'][$key]['#attributes']['class'][] = 'taxonomy-term-divider-bottom';
Chris@0 345 }
Chris@0 346 }
Chris@0 347
Chris@0 348 // Add an error class if this row contains a form error.
Chris@0 349 foreach ($errors as $error_key => $error) {
Chris@0 350 if (strpos($error_key, $key) === 0) {
Chris@0 351 $form['terms'][$key]['#attributes']['class'][] = 'error';
Chris@0 352 }
Chris@0 353 }
Chris@0 354 $row_position++;
Chris@0 355 }
Chris@0 356
Chris@14 357 $this->renderer->addCacheableDependency($form['terms'], $change_weight_access);
Chris@14 358 if ($change_weight_access->isAllowed()) {
Chris@14 359 if ($parent_fields) {
Chris@14 360 $form['terms']['#tabledrag'][] = [
Chris@14 361 'action' => 'match',
Chris@14 362 'relationship' => 'parent',
Chris@14 363 'group' => 'term-parent',
Chris@14 364 'subgroup' => 'term-parent',
Chris@14 365 'source' => 'term-id',
Chris@14 366 'hidden' => FALSE,
Chris@14 367 ];
Chris@14 368 $form['terms']['#tabledrag'][] = [
Chris@14 369 'action' => 'depth',
Chris@14 370 'relationship' => 'group',
Chris@14 371 'group' => 'term-depth',
Chris@14 372 'hidden' => FALSE,
Chris@14 373 ];
Chris@14 374 $form['terms']['#attached']['library'][] = 'taxonomy/drupal.taxonomy';
Chris@14 375 $form['terms']['#attached']['drupalSettings']['taxonomy'] = [
Chris@14 376 'backStep' => $back_step,
Chris@14 377 'forwardStep' => $forward_step,
Chris@14 378 ];
Chris@14 379 }
Chris@0 380 $form['terms']['#tabledrag'][] = [
Chris@14 381 'action' => 'order',
Chris@14 382 'relationship' => 'sibling',
Chris@14 383 'group' => 'term-weight',
Chris@0 384 ];
Chris@0 385 }
Chris@0 386
Chris@14 387 if (($taxonomy_vocabulary->getHierarchy() !== VocabularyInterface::HIERARCHY_MULTIPLE && count($tree) > 1) && $change_weight_access->isAllowed()) {
Chris@0 388 $form['actions'] = ['#type' => 'actions', '#tree' => FALSE];
Chris@0 389 $form['actions']['submit'] = [
Chris@0 390 '#type' => 'submit',
Chris@0 391 '#value' => $this->t('Save'),
Chris@0 392 '#button_type' => 'primary',
Chris@0 393 ];
Chris@0 394 $form['actions']['reset_alphabetical'] = [
Chris@0 395 '#type' => 'submit',
Chris@0 396 '#submit' => ['::submitReset'],
Chris@0 397 '#value' => $this->t('Reset to alphabetical'),
Chris@0 398 ];
Chris@0 399 }
Chris@0 400
Chris@0 401 $form['pager_pager'] = ['#type' => 'pager'];
Chris@0 402 return $form;
Chris@0 403 }
Chris@0 404
Chris@0 405 /**
Chris@0 406 * Form submission handler.
Chris@0 407 *
Chris@0 408 * Rather than using a textfield or weight field, this form depends entirely
Chris@0 409 * upon the order of form elements on the page to determine new weights.
Chris@0 410 *
Chris@0 411 * Because there might be hundreds or thousands of taxonomy terms that need to
Chris@0 412 * be ordered, terms are weighted from 0 to the number of terms in the
Chris@0 413 * vocabulary, rather than the standard -10 to 10 scale. Numbers are sorted
Chris@0 414 * lowest to highest, but are not necessarily sequential. Numbers may be
Chris@0 415 * skipped when a term has children so that reordering is minimal when a child
Chris@0 416 * is added or removed from a term.
Chris@0 417 *
Chris@0 418 * @param array $form
Chris@0 419 * An associative array containing the structure of the form.
Chris@0 420 * @param \Drupal\Core\Form\FormStateInterface $form_state
Chris@0 421 * The current state of the form.
Chris@0 422 */
Chris@0 423 public function submitForm(array &$form, FormStateInterface $form_state) {
Chris@0 424 // Sort term order based on weight.
Chris@0 425 uasort($form_state->getValue('terms'), ['Drupal\Component\Utility\SortArray', 'sortByWeightElement']);
Chris@0 426
Chris@0 427 $vocabulary = $form_state->get(['taxonomy', 'vocabulary']);
Chris@0 428 // Update the current hierarchy type as we go.
Chris@0 429 $hierarchy = VocabularyInterface::HIERARCHY_DISABLED;
Chris@0 430
Chris@0 431 $changed_terms = [];
Chris@0 432 $tree = $this->storageController->loadTree($vocabulary->id(), 0, NULL, TRUE);
Chris@0 433
Chris@0 434 if (empty($tree)) {
Chris@0 435 return;
Chris@0 436 }
Chris@0 437
Chris@0 438 // Build a list of all terms that need to be updated on previous pages.
Chris@0 439 $weight = 0;
Chris@0 440 $term = $tree[0];
Chris@0 441 while ($term->id() != $form['#first_tid']) {
Chris@0 442 if ($term->parents[0] == 0 && $term->getWeight() != $weight) {
Chris@0 443 $term->setWeight($weight);
Chris@0 444 $changed_terms[$term->id()] = $term;
Chris@0 445 }
Chris@0 446 $weight++;
Chris@0 447 $hierarchy = $term->parents[0] != 0 ? VocabularyInterface::HIERARCHY_SINGLE : $hierarchy;
Chris@0 448 $term = $tree[$weight];
Chris@0 449 }
Chris@0 450
Chris@0 451 // Renumber the current page weights and assign any new parents.
Chris@0 452 $level_weights = [];
Chris@0 453 foreach ($form_state->getValue('terms') as $tid => $values) {
Chris@0 454 if (isset($form['terms'][$tid]['#term'])) {
Chris@0 455 $term = $form['terms'][$tid]['#term'];
Chris@0 456 // Give terms at the root level a weight in sequence with terms on previous pages.
Chris@0 457 if ($values['term']['parent'] == 0 && $term->getWeight() != $weight) {
Chris@0 458 $term->setWeight($weight);
Chris@0 459 $changed_terms[$term->id()] = $term;
Chris@0 460 }
Chris@0 461 // Terms not at the root level can safely start from 0 because they're all on this page.
Chris@0 462 elseif ($values['term']['parent'] > 0) {
Chris@0 463 $level_weights[$values['term']['parent']] = isset($level_weights[$values['term']['parent']]) ? $level_weights[$values['term']['parent']] + 1 : 0;
Chris@0 464 if ($level_weights[$values['term']['parent']] != $term->getWeight()) {
Chris@0 465 $term->setWeight($level_weights[$values['term']['parent']]);
Chris@0 466 $changed_terms[$term->id()] = $term;
Chris@0 467 }
Chris@0 468 }
Chris@0 469 // Update any changed parents.
Chris@0 470 if ($values['term']['parent'] != $term->parents[0]) {
Chris@0 471 $term->parent->target_id = $values['term']['parent'];
Chris@0 472 $changed_terms[$term->id()] = $term;
Chris@0 473 }
Chris@0 474 $hierarchy = $term->parents[0] != 0 ? VocabularyInterface::HIERARCHY_SINGLE : $hierarchy;
Chris@0 475 $weight++;
Chris@0 476 }
Chris@0 477 }
Chris@0 478
Chris@0 479 // Build a list of all terms that need to be updated on following pages.
Chris@0 480 for ($weight; $weight < count($tree); $weight++) {
Chris@0 481 $term = $tree[$weight];
Chris@0 482 if ($term->parents[0] == 0 && $term->getWeight() != $weight) {
Chris@0 483 $term->parent->target_id = $term->parents[0];
Chris@0 484 $term->setWeight($weight);
Chris@0 485 $changed_terms[$term->id()] = $term;
Chris@0 486 }
Chris@0 487 $hierarchy = $term->parents[0] != 0 ? VocabularyInterface::HIERARCHY_SINGLE : $hierarchy;
Chris@0 488 }
Chris@0 489
Chris@0 490 // Save all updated terms.
Chris@0 491 foreach ($changed_terms as $term) {
Chris@0 492 $term->save();
Chris@0 493 }
Chris@0 494
Chris@0 495 // Update the vocabulary hierarchy to flat or single hierarchy.
Chris@0 496 if ($vocabulary->getHierarchy() != $hierarchy) {
Chris@0 497 $vocabulary->setHierarchy($hierarchy);
Chris@0 498 $vocabulary->save();
Chris@0 499 }
Chris@0 500 drupal_set_message($this->t('The configuration options have been saved.'));
Chris@0 501 }
Chris@0 502
Chris@0 503 /**
Chris@0 504 * Redirects to confirmation form for the reset action.
Chris@0 505 */
Chris@0 506 public function submitReset(array &$form, FormStateInterface $form_state) {
Chris@0 507 /** @var $vocabulary \Drupal\taxonomy\VocabularyInterface */
Chris@0 508 $vocabulary = $form_state->get(['taxonomy', 'vocabulary']);
Chris@0 509 $form_state->setRedirectUrl($vocabulary->urlInfo('reset-form'));
Chris@0 510 }
Chris@0 511
Chris@0 512 }