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