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