Chris@0: 'entity.manager']; Chris@0: Chris@0: /** Chris@0: * The module handler service. Chris@0: * Chris@0: * @var \Drupal\Core\Extension\ModuleHandlerInterface Chris@0: */ Chris@0: protected $moduleHandler; Chris@0: Chris@0: /** Chris@0: * The entity manager. Chris@0: * Chris@0: * @var \Drupal\Core\Entity\EntityManagerInterface Chris@0: */ Chris@18: protected $entityTypeManager; Chris@0: Chris@0: /** Chris@0: * The term storage handler. Chris@0: * Chris@0: * @var \Drupal\taxonomy\TermStorageInterface Chris@0: */ Chris@0: protected $storageController; Chris@0: Chris@0: /** Chris@14: * The term list builder. Chris@14: * Chris@14: * @var \Drupal\Core\Entity\EntityListBuilderInterface Chris@14: */ Chris@14: protected $termListBuilder; Chris@14: Chris@14: /** Chris@14: * The renderer service. Chris@14: * Chris@14: * @var \Drupal\Core\Render\RendererInterface Chris@14: */ Chris@14: protected $renderer; Chris@14: Chris@14: /** Chris@18: * The entity repository. Chris@18: * Chris@18: * @var \Drupal\Core\Entity\EntityRepositoryInterface Chris@18: */ Chris@18: protected $entityRepository; Chris@18: Chris@18: /** Chris@0: * Constructs an OverviewTerms object. Chris@0: * Chris@0: * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler Chris@0: * The module handler service. Chris@18: * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager Chris@18: * The entity type manager service. Chris@14: * @param \Drupal\Core\Render\RendererInterface $renderer Chris@14: * The renderer service. Chris@18: * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository Chris@18: * The entity repository. Chris@0: */ Chris@18: public function __construct(ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer = NULL, EntityRepositoryInterface $entity_repository = NULL) { Chris@0: $this->moduleHandler = $module_handler; Chris@18: $this->entityTypeManager = $entity_type_manager; Chris@18: $this->storageController = $entity_type_manager->getStorage('taxonomy_term'); Chris@18: $this->termListBuilder = $entity_type_manager->getListBuilder('taxonomy_term'); Chris@14: $this->renderer = $renderer ?: \Drupal::service('renderer'); Chris@18: if (!$entity_repository) { Chris@18: @trigger_error('Calling OverviewTerms::__construct() with the $entity_repository argument is supported in drupal:8.7.0 and will be required before drupal:9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); Chris@18: $entity_repository = \Drupal::service('entity.repository'); Chris@18: } Chris@18: $this->entityRepository = $entity_repository; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container) { Chris@0: return new static( Chris@0: $container->get('module_handler'), Chris@18: $container->get('entity_type.manager'), Chris@18: $container->get('renderer'), Chris@18: $container->get('entity.repository') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getFormId() { Chris@0: return 'taxonomy_overview_terms'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Form constructor. Chris@0: * Chris@0: * Display a tree of all the terms in a vocabulary, with options to edit Chris@0: * each one. The form is made drag and drop by the theme function. Chris@0: * Chris@0: * @param array $form Chris@0: * An associative array containing the structure of the form. Chris@0: * @param \Drupal\Core\Form\FormStateInterface $form_state Chris@0: * The current state of the form. Chris@0: * @param \Drupal\taxonomy\VocabularyInterface $taxonomy_vocabulary Chris@0: * The vocabulary to display the overview form for. Chris@0: * Chris@0: * @return array Chris@0: * The form structure. Chris@0: */ Chris@0: public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL) { Chris@0: // @todo Remove global variables when https://www.drupal.org/node/2044435 is Chris@0: // in. Chris@0: global $pager_page_array, $pager_total, $pager_total_items; Chris@0: Chris@0: $form_state->set(['taxonomy', 'vocabulary'], $taxonomy_vocabulary); Chris@18: $vocabulary_hierarchy = $this->storageController->getVocabularyHierarchyType($taxonomy_vocabulary->id()); Chris@0: $parent_fields = FALSE; Chris@0: Chris@0: $page = $this->getRequest()->query->get('page') ?: 0; Chris@0: // Number of terms per page. Chris@0: $page_increment = $this->config('taxonomy.settings')->get('terms_per_page_admin'); Chris@0: // Elements shown on this page. Chris@0: $page_entries = 0; Chris@0: // Elements at the root level before this page. Chris@0: $before_entries = 0; Chris@0: // Elements at the root level after this page. Chris@0: $after_entries = 0; Chris@0: // Elements at the root level on this page. Chris@0: $root_entries = 0; Chris@0: Chris@0: // Terms from previous and next pages are shown if the term tree would have Chris@0: // been cut in the middle. Keep track of how many extra terms we show on Chris@0: // each page of terms. Chris@0: $back_step = NULL; Chris@0: $forward_step = 0; Chris@0: Chris@0: // An array of the terms to be displayed on this page. Chris@0: $current_page = []; Chris@0: Chris@0: $delta = 0; Chris@0: $term_deltas = []; Chris@0: $tree = $this->storageController->loadTree($taxonomy_vocabulary->id(), 0, NULL, TRUE); Chris@0: $tree_index = 0; Chris@0: do { Chris@0: // In case this tree is completely empty. Chris@0: if (empty($tree[$tree_index])) { Chris@0: break; Chris@0: } Chris@0: $delta++; Chris@0: // Count entries before the current page. Chris@0: if ($page && ($page * $page_increment) > $before_entries && !isset($back_step)) { Chris@0: $before_entries++; Chris@0: continue; Chris@0: } Chris@0: // Count entries after the current page. Chris@0: elseif ($page_entries > $page_increment && isset($complete_tree)) { Chris@0: $after_entries++; Chris@0: continue; Chris@0: } Chris@0: Chris@0: // Do not let a term start the page that is not at the root. Chris@0: $term = $tree[$tree_index]; Chris@0: if (isset($term->depth) && ($term->depth > 0) && !isset($back_step)) { Chris@0: $back_step = 0; Chris@0: while ($pterm = $tree[--$tree_index]) { Chris@0: $before_entries--; Chris@0: $back_step++; Chris@0: if ($pterm->depth == 0) { Chris@0: $tree_index--; Chris@0: // Jump back to the start of the root level parent. Chris@0: continue 2; Chris@0: } Chris@0: } Chris@0: } Chris@0: $back_step = isset($back_step) ? $back_step : 0; Chris@0: Chris@0: // Continue rendering the tree until we reach the a new root item. Chris@0: if ($page_entries >= $page_increment + $back_step + 1 && $term->depth == 0 && $root_entries > 1) { Chris@0: $complete_tree = TRUE; Chris@0: // This new item at the root level is the first item on the next page. Chris@0: $after_entries++; Chris@0: continue; Chris@0: } Chris@0: if ($page_entries >= $page_increment + $back_step) { Chris@0: $forward_step++; Chris@0: } Chris@0: Chris@0: // Finally, if we've gotten down this far, we're rendering a term on this Chris@0: // page. Chris@0: $page_entries++; Chris@0: $term_deltas[$term->id()] = isset($term_deltas[$term->id()]) ? $term_deltas[$term->id()] + 1 : 0; Chris@0: $key = 'tid:' . $term->id() . ':' . $term_deltas[$term->id()]; Chris@0: Chris@0: // Keep track of the first term displayed on this page. Chris@0: if ($page_entries == 1) { Chris@0: $form['#first_tid'] = $term->id(); Chris@0: } Chris@0: // Keep a variable to make sure at least 2 root elements are displayed. Chris@0: if ($term->parents[0] == 0) { Chris@0: $root_entries++; Chris@0: } Chris@0: $current_page[$key] = $term; Chris@0: } while (isset($tree[++$tree_index])); Chris@0: Chris@0: // Because we didn't use a pager query, set the necessary pager variables. Chris@0: $total_entries = $before_entries + $page_entries + $after_entries; Chris@0: $pager_total_items[0] = $total_entries; Chris@0: $pager_page_array[0] = $page; Chris@0: $pager_total[0] = ceil($total_entries / $page_increment); Chris@0: Chris@0: // If this form was already submitted once, it's probably hit a validation Chris@0: // error. Ensure the form is rebuilt in the same order as the user Chris@0: // submitted. Chris@0: $user_input = $form_state->getUserInput(); Chris@0: if (!empty($user_input)) { Chris@0: // Get the POST order. Chris@0: $order = array_flip(array_keys($user_input['terms'])); Chris@0: // Update our form with the new order. Chris@0: $current_page = array_merge($order, $current_page); Chris@0: foreach ($current_page as $key => $term) { Chris@0: // Verify this is a term for the current page and set at the current Chris@0: // depth. Chris@0: if (is_array($user_input['terms'][$key]) && is_numeric($user_input['terms'][$key]['term']['tid'])) { Chris@0: $current_page[$key]->depth = $user_input['terms'][$key]['term']['depth']; Chris@0: } Chris@0: else { Chris@0: unset($current_page[$key]); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@18: $args = [ Chris@18: '%capital_name' => Unicode::ucfirst($taxonomy_vocabulary->label()), Chris@18: '%name' => $taxonomy_vocabulary->label(), Chris@18: ]; Chris@18: if ($this->currentUser()->hasPermission('administer taxonomy') || $this->currentUser()->hasPermission('edit terms in ' . $taxonomy_vocabulary->id())) { Chris@18: switch ($vocabulary_hierarchy) { Chris@18: case VocabularyInterface::HIERARCHY_DISABLED: Chris@18: $help_message = $this->t('You can reorganize the terms in %capital_name using their drag-and-drop handles, and group terms under a parent term by sliding them under and to the right of the parent.', $args); Chris@18: break; Chris@18: case VocabularyInterface::HIERARCHY_SINGLE: Chris@18: $help_message = $this->t('%capital_name contains terms grouped under parent terms. You can reorganize the terms in %capital_name using their drag-and-drop handles.', $args); Chris@18: break; Chris@18: case VocabularyInterface::HIERARCHY_MULTIPLE: Chris@18: $help_message = $this->t('%capital_name contains terms with multiple parents. Drag and drop of terms with multiple parents is not supported, but you can re-enable drag-and-drop support by editing each term to include only a single parent.', $args); Chris@18: break; Chris@18: } Chris@18: } Chris@18: else { Chris@18: switch ($vocabulary_hierarchy) { Chris@18: case VocabularyInterface::HIERARCHY_DISABLED: Chris@18: $help_message = $this->t('%capital_name contains the following terms.', $args); Chris@18: break; Chris@18: case VocabularyInterface::HIERARCHY_SINGLE: Chris@18: $help_message = $this->t('%capital_name contains terms grouped under parent terms', $args); Chris@18: break; Chris@18: case VocabularyInterface::HIERARCHY_MULTIPLE: Chris@18: $help_message = $this->t('%capital_name contains terms with multiple parents.', $args); Chris@18: break; Chris@18: } Chris@18: } Chris@18: Chris@18: // Get the IDs of the terms edited on the current page which have pending Chris@18: // revisions. Chris@18: $edited_term_ids = array_map(function ($item) { Chris@18: return $item->id(); Chris@18: }, $current_page); Chris@18: $pending_term_ids = array_intersect($this->storageController->getTermIdsWithPendingRevisions(), $edited_term_ids); Chris@18: if ($pending_term_ids) { Chris@18: $help_message = $this->formatPlural( Chris@18: count($pending_term_ids), Chris@18: '%capital_name contains 1 term with pending revisions. Drag and drop of terms with pending revisions is not supported, but you can re-enable drag-and-drop support by getting each term to a published state.', Chris@18: '%capital_name contains @count terms with pending revisions. Drag and drop of terms with pending revisions is not supported, but you can re-enable drag-and-drop support by getting each term to a published state.', Chris@18: $args Chris@18: ); Chris@18: } Chris@18: Chris@18: // Only allow access to change parents and reorder the tree if there are no Chris@18: // pending revisions and there are no terms with multiple parents. Chris@18: $update_tree_access = AccessResult::allowedIf(empty($pending_term_ids) && $vocabulary_hierarchy !== VocabularyInterface::HIERARCHY_MULTIPLE); Chris@18: Chris@18: $form['help'] = [ Chris@18: '#type' => 'container', Chris@18: 'message' => ['#markup' => $help_message], Chris@18: ]; Chris@18: if (!$update_tree_access->isAllowed()) { Chris@18: $form['help']['#attributes']['class'] = ['messages', 'messages--warning']; Chris@18: } Chris@18: Chris@0: $errors = $form_state->getErrors(); Chris@0: $row_position = 0; Chris@0: // Build the actual form. Chris@18: $access_control_handler = $this->entityTypeManager->getAccessControlHandler('taxonomy_term'); Chris@14: $create_access = $access_control_handler->createAccess($taxonomy_vocabulary->id(), NULL, [], TRUE); Chris@14: if ($create_access->isAllowed()) { Chris@14: $empty = $this->t('No terms available. Add term.', [':link' => Url::fromRoute('entity.taxonomy_term.add_form', ['taxonomy_vocabulary' => $taxonomy_vocabulary->id()])->toString()]); Chris@14: } Chris@14: else { Chris@14: $empty = $this->t('No terms available.'); Chris@14: } Chris@0: $form['terms'] = [ Chris@0: '#type' => 'table', Chris@14: '#empty' => $empty, Chris@16: '#header' => [ Chris@16: 'term' => $this->t('Name'), Chris@16: 'operations' => $this->t('Operations'), Chris@18: 'weight' => $update_tree_access->isAllowed() ? $this->t('Weight') : NULL, Chris@16: ], Chris@0: '#attributes' => [ Chris@0: 'id' => 'taxonomy', Chris@0: ], Chris@0: ]; Chris@14: $this->renderer->addCacheableDependency($form['terms'], $create_access); Chris@14: Chris@0: foreach ($current_page as $key => $term) { Chris@16: $form['terms'][$key] = [ Chris@16: 'term' => [], Chris@16: 'operations' => [], Chris@18: 'weight' => $update_tree_access->isAllowed() ? [] : NULL, Chris@16: ]; Chris@0: /** @var $term \Drupal\Core\Entity\EntityInterface */ Chris@18: $term = $this->entityRepository->getTranslationFromContext($term); Chris@0: $form['terms'][$key]['#term'] = $term; Chris@0: $indentation = []; Chris@0: if (isset($term->depth) && $term->depth > 0) { Chris@0: $indentation = [ Chris@0: '#theme' => 'indentation', Chris@0: '#size' => $term->depth, Chris@0: ]; Chris@0: } Chris@0: $form['terms'][$key]['term'] = [ Chris@18: '#prefix' => !empty($indentation) ? $this->renderer->render($indentation) : '', Chris@0: '#type' => 'link', Chris@0: '#title' => $term->getName(), Chris@18: '#url' => $term->toUrl(), Chris@0: ]; Chris@18: Chris@18: // Add a special class for terms with pending revision so we can highlight Chris@18: // them in the form. Chris@18: $form['terms'][$key]['#attributes']['class'] = []; Chris@18: if (in_array($term->id(), $pending_term_ids)) { Chris@18: $form['terms'][$key]['#attributes']['class'][] = 'color-warning'; Chris@18: $form['terms'][$key]['#attributes']['class'][] = 'taxonomy-term--pending-revision'; Chris@18: } Chris@18: Chris@18: if ($update_tree_access->isAllowed() && count($tree) > 1) { Chris@0: $parent_fields = TRUE; Chris@0: $form['terms'][$key]['term']['tid'] = [ Chris@0: '#type' => 'hidden', Chris@0: '#value' => $term->id(), Chris@0: '#attributes' => [ Chris@0: 'class' => ['term-id'], Chris@0: ], Chris@0: ]; Chris@0: $form['terms'][$key]['term']['parent'] = [ Chris@0: '#type' => 'hidden', Chris@0: // Yes, default_value on a hidden. It needs to be changeable by the Chris@0: // javascript. Chris@0: '#default_value' => $term->parents[0], Chris@0: '#attributes' => [ Chris@0: 'class' => ['term-parent'], Chris@0: ], Chris@0: ]; Chris@0: $form['terms'][$key]['term']['depth'] = [ Chris@0: '#type' => 'hidden', Chris@0: // Same as above, the depth is modified by javascript, so it's a Chris@0: // default_value. Chris@0: '#default_value' => $term->depth, Chris@0: '#attributes' => [ Chris@0: 'class' => ['term-depth'], Chris@0: ], Chris@0: ]; Chris@0: } Chris@14: $update_access = $term->access('update', NULL, TRUE); Chris@18: $update_tree_access = $update_tree_access->andIf($update_access); Chris@14: Chris@18: if ($update_tree_access->isAllowed()) { Chris@14: $form['terms'][$key]['weight'] = [ Chris@14: '#type' => 'weight', Chris@14: '#delta' => $delta, Chris@14: '#title' => $this->t('Weight for added term'), Chris@14: '#title_display' => 'invisible', Chris@14: '#default_value' => $term->getWeight(), Chris@14: '#attributes' => ['class' => ['term-weight']], Chris@0: ]; Chris@0: } Chris@14: Chris@14: if ($operations = $this->termListBuilder->getOperations($term)) { Chris@14: $form['terms'][$key]['operations'] = [ Chris@14: '#type' => 'operations', Chris@14: '#links' => $operations, Chris@14: ]; Chris@14: } Chris@0: Chris@0: if ($parent_fields) { Chris@0: $form['terms'][$key]['#attributes']['class'][] = 'draggable'; Chris@0: } Chris@0: Chris@0: // Add classes that mark which terms belong to previous and next pages. Chris@0: if ($row_position < $back_step || $row_position >= $page_entries - $forward_step) { Chris@0: $form['terms'][$key]['#attributes']['class'][] = 'taxonomy-term-preview'; Chris@0: } Chris@0: Chris@0: if ($row_position !== 0 && $row_position !== count($tree) - 1) { Chris@0: if ($row_position == $back_step - 1 || $row_position == $page_entries - $forward_step - 1) { Chris@0: $form['terms'][$key]['#attributes']['class'][] = 'taxonomy-term-divider-top'; Chris@0: } Chris@0: elseif ($row_position == $back_step || $row_position == $page_entries - $forward_step) { Chris@0: $form['terms'][$key]['#attributes']['class'][] = 'taxonomy-term-divider-bottom'; Chris@0: } Chris@0: } Chris@0: Chris@0: // Add an error class if this row contains a form error. Chris@0: foreach ($errors as $error_key => $error) { Chris@0: if (strpos($error_key, $key) === 0) { Chris@0: $form['terms'][$key]['#attributes']['class'][] = 'error'; Chris@0: } Chris@0: } Chris@0: $row_position++; Chris@0: } Chris@0: Chris@18: $this->renderer->addCacheableDependency($form['terms'], $update_tree_access); Chris@18: if ($update_tree_access->isAllowed()) { Chris@14: if ($parent_fields) { Chris@14: $form['terms']['#tabledrag'][] = [ Chris@14: 'action' => 'match', Chris@14: 'relationship' => 'parent', Chris@14: 'group' => 'term-parent', Chris@14: 'subgroup' => 'term-parent', Chris@14: 'source' => 'term-id', Chris@14: 'hidden' => FALSE, Chris@14: ]; Chris@14: $form['terms']['#tabledrag'][] = [ Chris@14: 'action' => 'depth', Chris@14: 'relationship' => 'group', Chris@14: 'group' => 'term-depth', Chris@14: 'hidden' => FALSE, Chris@14: ]; Chris@14: $form['terms']['#attached']['library'][] = 'taxonomy/drupal.taxonomy'; Chris@14: $form['terms']['#attached']['drupalSettings']['taxonomy'] = [ Chris@14: 'backStep' => $back_step, Chris@14: 'forwardStep' => $forward_step, Chris@14: ]; Chris@14: } Chris@0: $form['terms']['#tabledrag'][] = [ Chris@14: 'action' => 'order', Chris@14: 'relationship' => 'sibling', Chris@14: 'group' => 'term-weight', Chris@0: ]; Chris@0: } Chris@0: Chris@18: if ($update_tree_access->isAllowed() && count($tree) > 1) { Chris@0: $form['actions'] = ['#type' => 'actions', '#tree' => FALSE]; Chris@0: $form['actions']['submit'] = [ Chris@0: '#type' => 'submit', Chris@0: '#value' => $this->t('Save'), Chris@0: '#button_type' => 'primary', Chris@0: ]; Chris@0: $form['actions']['reset_alphabetical'] = [ Chris@0: '#type' => 'submit', Chris@0: '#submit' => ['::submitReset'], Chris@0: '#value' => $this->t('Reset to alphabetical'), Chris@0: ]; Chris@0: } Chris@0: Chris@0: $form['pager_pager'] = ['#type' => 'pager']; Chris@0: return $form; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Form submission handler. Chris@0: * Chris@0: * Rather than using a textfield or weight field, this form depends entirely Chris@0: * upon the order of form elements on the page to determine new weights. Chris@0: * Chris@0: * Because there might be hundreds or thousands of taxonomy terms that need to Chris@0: * be ordered, terms are weighted from 0 to the number of terms in the Chris@0: * vocabulary, rather than the standard -10 to 10 scale. Numbers are sorted Chris@0: * lowest to highest, but are not necessarily sequential. Numbers may be Chris@0: * skipped when a term has children so that reordering is minimal when a child Chris@0: * is added or removed from a term. Chris@0: * Chris@0: * @param array $form Chris@0: * An associative array containing the structure of the form. Chris@0: * @param \Drupal\Core\Form\FormStateInterface $form_state Chris@0: * The current state of the form. Chris@0: */ Chris@0: public function submitForm(array &$form, FormStateInterface $form_state) { Chris@0: // Sort term order based on weight. Chris@0: uasort($form_state->getValue('terms'), ['Drupal\Component\Utility\SortArray', 'sortByWeightElement']); Chris@0: Chris@0: $vocabulary = $form_state->get(['taxonomy', 'vocabulary']); Chris@0: $changed_terms = []; Chris@0: $tree = $this->storageController->loadTree($vocabulary->id(), 0, NULL, TRUE); Chris@0: Chris@0: if (empty($tree)) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // Build a list of all terms that need to be updated on previous pages. Chris@0: $weight = 0; Chris@0: $term = $tree[0]; Chris@0: while ($term->id() != $form['#first_tid']) { Chris@0: if ($term->parents[0] == 0 && $term->getWeight() != $weight) { Chris@0: $term->setWeight($weight); Chris@0: $changed_terms[$term->id()] = $term; Chris@0: } Chris@0: $weight++; Chris@0: $term = $tree[$weight]; Chris@0: } Chris@0: Chris@0: // Renumber the current page weights and assign any new parents. Chris@0: $level_weights = []; Chris@0: foreach ($form_state->getValue('terms') as $tid => $values) { Chris@0: if (isset($form['terms'][$tid]['#term'])) { Chris@0: $term = $form['terms'][$tid]['#term']; Chris@0: // Give terms at the root level a weight in sequence with terms on previous pages. Chris@0: if ($values['term']['parent'] == 0 && $term->getWeight() != $weight) { Chris@0: $term->setWeight($weight); Chris@0: $changed_terms[$term->id()] = $term; Chris@0: } Chris@0: // Terms not at the root level can safely start from 0 because they're all on this page. Chris@0: elseif ($values['term']['parent'] > 0) { Chris@0: $level_weights[$values['term']['parent']] = isset($level_weights[$values['term']['parent']]) ? $level_weights[$values['term']['parent']] + 1 : 0; Chris@0: if ($level_weights[$values['term']['parent']] != $term->getWeight()) { Chris@0: $term->setWeight($level_weights[$values['term']['parent']]); Chris@0: $changed_terms[$term->id()] = $term; Chris@0: } Chris@0: } Chris@0: // Update any changed parents. Chris@0: if ($values['term']['parent'] != $term->parents[0]) { Chris@0: $term->parent->target_id = $values['term']['parent']; Chris@0: $changed_terms[$term->id()] = $term; Chris@0: } Chris@0: $weight++; Chris@0: } Chris@0: } Chris@0: Chris@0: // Build a list of all terms that need to be updated on following pages. Chris@0: for ($weight; $weight < count($tree); $weight++) { Chris@0: $term = $tree[$weight]; Chris@0: if ($term->parents[0] == 0 && $term->getWeight() != $weight) { Chris@0: $term->parent->target_id = $term->parents[0]; Chris@0: $term->setWeight($weight); Chris@0: $changed_terms[$term->id()] = $term; Chris@0: } Chris@0: } Chris@0: Chris@18: if (!empty($changed_terms)) { Chris@18: $pending_term_ids = $this->storageController->getTermIdsWithPendingRevisions(); Chris@18: Chris@18: // Force a form rebuild if any of the changed terms has a pending Chris@18: // revision. Chris@18: if (array_intersect_key(array_flip($pending_term_ids), $changed_terms)) { Chris@18: $this->messenger()->addError($this->t('The terms with updated parents have been modified by another user, the changes could not be saved.')); Chris@18: $form_state->setRebuild(); Chris@18: Chris@18: return; Chris@18: } Chris@18: Chris@18: // Save all updated terms. Chris@18: foreach ($changed_terms as $term) { Chris@18: $term->save(); Chris@18: } Chris@18: Chris@18: $this->messenger()->addStatus($this->t('The configuration options have been saved.')); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Redirects to confirmation form for the reset action. Chris@0: */ Chris@0: public function submitReset(array &$form, FormStateInterface $form_state) { Chris@0: /** @var $vocabulary \Drupal\taxonomy\VocabularyInterface */ Chris@0: $vocabulary = $form_state->get(['taxonomy', 'vocabulary']); Chris@18: $form_state->setRedirectUrl($vocabulary->toUrl('reset-form')); Chris@0: } Chris@0: Chris@0: }