Chris@0: moduleHandler = $module_handler; Chris@0: $this->entityManager = $entity_manager; Chris@0: $this->storageController = $entity_manager->getStorage('taxonomy_term'); 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@0: $container->get('entity.manager') 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@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@0: $errors = $form_state->getErrors(); Chris@0: $destination = $this->getDestinationArray(); Chris@0: $row_position = 0; Chris@0: // Build the actual form. Chris@0: $form['terms'] = [ Chris@0: '#type' => 'table', Chris@0: '#header' => [$this->t('Name'), $this->t('Weight'), $this->t('Operations')], Chris@0: '#empty' => $this->t('No terms available. Add term.', [':link' => $this->url('entity.taxonomy_term.add_form', ['taxonomy_vocabulary' => $taxonomy_vocabulary->id()])]), Chris@0: '#attributes' => [ Chris@0: 'id' => 'taxonomy', Chris@0: ], Chris@0: ]; Chris@0: foreach ($current_page as $key => $term) { Chris@0: /** @var $term \Drupal\Core\Entity\EntityInterface */ Chris@0: $term = $this->entityManager->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@0: '#prefix' => !empty($indentation) ? \Drupal::service('renderer')->render($indentation) : '', Chris@0: '#type' => 'link', Chris@0: '#title' => $term->getName(), Chris@0: '#url' => $term->urlInfo(), Chris@0: ]; Chris@0: if ($taxonomy_vocabulary->getHierarchy() != VocabularyInterface::HIERARCHY_MULTIPLE && 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@0: $form['terms'][$key]['weight'] = [ Chris@0: '#type' => 'weight', Chris@0: '#delta' => $delta, Chris@0: '#title' => $this->t('Weight for added term'), Chris@0: '#title_display' => 'invisible', Chris@0: '#default_value' => $term->getWeight(), Chris@0: '#attributes' => [ Chris@0: 'class' => ['term-weight'], Chris@0: ], Chris@0: ]; Chris@0: $operations = [ Chris@0: 'edit' => [ Chris@0: 'title' => $this->t('Edit'), Chris@0: 'query' => $destination, Chris@0: 'url' => $term->urlInfo('edit-form'), Chris@0: ], Chris@0: 'delete' => [ Chris@0: 'title' => $this->t('Delete'), Chris@0: 'query' => $destination, Chris@0: 'url' => $term->urlInfo('delete-form'), Chris@0: ], Chris@0: ]; Chris@0: if ($this->moduleHandler->moduleExists('content_translation') && content_translation_translate_access($term)->isAllowed()) { Chris@0: $operations['translate'] = [ Chris@0: 'title' => $this->t('Translate'), Chris@0: 'query' => $destination, Chris@0: 'url' => $term->urlInfo('drupal:content-translation-overview'), Chris@0: ]; Chris@0: } Chris@0: $form['terms'][$key]['operations'] = [ Chris@0: '#type' => 'operations', Chris@0: '#links' => $operations, Chris@0: ]; Chris@0: Chris@0: $form['terms'][$key]['#attributes']['class'] = []; 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@0: if ($parent_fields) { Chris@0: $form['terms']['#tabledrag'][] = [ Chris@0: 'action' => 'match', Chris@0: 'relationship' => 'parent', Chris@0: 'group' => 'term-parent', Chris@0: 'subgroup' => 'term-parent', Chris@0: 'source' => 'term-id', Chris@0: 'hidden' => FALSE, Chris@0: ]; Chris@0: $form['terms']['#tabledrag'][] = [ Chris@0: 'action' => 'depth', Chris@0: 'relationship' => 'group', Chris@0: 'group' => 'term-depth', Chris@0: 'hidden' => FALSE, Chris@0: ]; Chris@0: $form['terms']['#attached']['library'][] = 'taxonomy/drupal.taxonomy'; Chris@0: $form['terms']['#attached']['drupalSettings']['taxonomy'] = [ Chris@0: 'backStep' => $back_step, Chris@0: 'forwardStep' => $forward_step, Chris@0: ]; Chris@0: } Chris@0: $form['terms']['#tabledrag'][] = [ Chris@0: 'action' => 'order', Chris@0: 'relationship' => 'sibling', Chris@0: 'group' => 'term-weight', Chris@0: ]; Chris@0: Chris@0: if ($taxonomy_vocabulary->getHierarchy() != VocabularyInterface::HIERARCHY_MULTIPLE && 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: // Update the current hierarchy type as we go. Chris@0: $hierarchy = VocabularyInterface::HIERARCHY_DISABLED; Chris@0: 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: $hierarchy = $term->parents[0] != 0 ? VocabularyInterface::HIERARCHY_SINGLE : $hierarchy; 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: $hierarchy = $term->parents[0] != 0 ? VocabularyInterface::HIERARCHY_SINGLE : $hierarchy; 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: $hierarchy = $term->parents[0] != 0 ? VocabularyInterface::HIERARCHY_SINGLE : $hierarchy; Chris@0: } Chris@0: Chris@0: // Save all updated terms. Chris@0: foreach ($changed_terms as $term) { Chris@0: $term->save(); Chris@0: } Chris@0: Chris@0: // Update the vocabulary hierarchy to flat or single hierarchy. Chris@0: if ($vocabulary->getHierarchy() != $hierarchy) { Chris@0: $vocabulary->setHierarchy($hierarchy); Chris@0: $vocabulary->save(); Chris@0: } Chris@0: drupal_set_message($this->t('The configuration options have been saved.')); 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@0: $form_state->setRedirectUrl($vocabulary->urlInfo('reset-form')); Chris@0: } Chris@0: Chris@0: }