OverviewTerms::buildForm

public OverviewTerms::buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL)

Form constructor.

Display a tree of all the terms in a vocabulary, with options to edit each one. The form is made drag and drop by the theme function.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

\Drupal\taxonomy\VocabularyInterface $taxonomy_vocabulary: The vocabulary to display the overview form for.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

core/modules/taxonomy/src/Form/OverviewTerms.php, line 77

Class

OverviewTerms
Provides terms overview form for a taxonomy vocabulary.

Namespace

Drupal\taxonomy\Form

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL) {
  // @todo Remove global variables when https://www.drupal.org/node/2044435 is
  //   in.
  global $pager_page_array, $pager_total, $pager_total_items;
 
  $form_state->set(['taxonomy', 'vocabulary'], $taxonomy_vocabulary);
  $parent_fields = FALSE;
 
  $page = $this->getRequest()->query->get('page') ? : 0;
  // Number of terms per page.
  $page_increment = $this->config('taxonomy.settings')->get('terms_per_page_admin');
  // Elements shown on this page.
  $page_entries = 0;
  // Elements at the root level before this page.
  $before_entries = 0;
  // Elements at the root level after this page.
  $after_entries = 0;
  // Elements at the root level on this page.
  $root_entries = 0;
 
  // Terms from previous and next pages are shown if the term tree would have
  // been cut in the middle. Keep track of how many extra terms we show on
  // each page of terms.
  $back_step = NULL;
  $forward_step = 0;
 
  // An array of the terms to be displayed on this page.
  $current_page = array();
 
  $delta = 0;
  $term_deltas = array();
  $tree = $this->storageController->loadTree($taxonomy_vocabulary->id(), 0, NULL, TRUE);
  $tree_index = 0;
  do {
    // In case this tree is completely empty.
    if (empty($tree[$tree_index])) {
      break;
    }
    $delta++;
    // Count entries before the current page.
    if ($page && ($page * $page_increment) > $before_entries && !isset($back_step)) {
      $before_entries++;
      continue;
    }
    // Count entries after the current page.
    elseif ($page_entries > $page_increment && isset($complete_tree)) {
      $after_entries++;
      continue;
    }
 
    // Do not let a term start the page that is not at the root.
    $term = $tree[$tree_index];
    if (isset($term->depth) && ($term->depth > 0) && !isset($back_step)) {
      $back_step = 0;
      while ($pterm = $tree[--$tree_index]) {
        $before_entries--;
        $back_step++;
        if ($pterm->depth == 0) {
          $tree_index--;
          // Jump back to the start of the root level parent.
          continue 2;
        }
      }
    }
    $back_step = isset($back_step) ? $back_step : 0;
 
    // Continue rendering the tree until we reach the a new root item.
    if ($page_entries >= $page_increment + $back_step + 1 && $term->depth == 0 && $root_entries > 1) {
      $complete_tree = TRUE;
      // This new item at the root level is the first item on the next page.
      $after_entries++;
      continue;
    }
    if ($page_entries >= $page_increment + $back_step) {
      $forward_step++;
    }
 
    // Finally, if we've gotten down this far, we're rendering a term on this
    // page.
    $page_entries++;
    $term_deltas[$term->id()] = isset($term_deltas[$term->id()]) ? $term_deltas[$term->id()] + 1 : 0;
    $key = 'tid:' . $term->id() . ':' . $term_deltas[$term->id()];
 
    // Keep track of the first term displayed on this page.
    if ($page_entries == 1) {
      $form['#first_tid'] = $term->id();
    }
    // Keep a variable to make sure at least 2 root elements are displayed.
    if ($term->parents[0] == 0) {
      $root_entries++;
    }
    $current_page[$key] = $term;
  } while (isset($tree[++$tree_index]));
 
  // Because we didn't use a pager query, set the necessary pager variables.
  $total_entries = $before_entries + $page_entries + $after_entries;
  $pager_total_items[0] = $total_entries;
  $pager_page_array[0] = $page;
  $pager_total[0] = ceil($total_entries / $page_increment);
 
  // If this form was already submitted once, it's probably hit a validation
  // error. Ensure the form is rebuilt in the same order as the user
  // submitted.
  $user_input = $form_state->getUserInput();
  if (!empty($user_input)) {
    // Get the POST order.
    $order = array_flip(array_keys($user_input['terms']));
    // Update our form with the new order.
    $current_page = array_merge($order, $current_page);
    foreach ($current_page as $key => $term) {
      // Verify this is a term for the current page and set at the current
      // depth.
      if (is_array($user_input['terms'][$key]) && is_numeric($user_input['terms'][$key]['term']['tid'])) {
        $current_page[$key]->depth = $user_input['terms'][$key]['term']['depth'];
      }
      else {
        unset($current_page[$key]);
      }
    }
  }
 
  $errors = $form_state->getErrors();
  $destination = $this->getDestinationArray();
  $row_position = 0;
  // Build the actual form.
  $form['terms'] = array(
    '#type' => 'table',
    '#header' => array($this->t('Name'), $this->t('Weight'), $this->t('Operations')),
    '#empty' => $this->t('No terms available. <a href=":link">Add term</a>.', array(':link' => $this->url('entity.taxonomy_term.add_form', array('taxonomy_vocabulary' => $taxonomy_vocabulary->id())))),
    '#attributes' => array(
      'id' => 'taxonomy',
    ),
  );
  foreach ($current_page as $key => $term) {
    /** @var $term \Drupal\Core\Entity\EntityInterface */
    $form['terms'][$key]['#term'] = $term;
    $indentation = array();
    if (isset($term->depth) && $term->depth > 0) {
      $indentation = array(
        '#theme' => 'indentation',
        '#size' => $term->depth,
      );
    }
    $form['terms'][$key]['term'] = array(
      '#prefix' => !empty($indentation) ? drupal_render($indentation) : '',
      '#type' => 'link',
      '#title' => $term->getName(),
      '#url' => $term->urlInfo(),
    );
    if ($taxonomy_vocabulary->getHierarchy() != VocabularyInterface::HIERARCHY_MULTIPLE && count($tree) > 1) {
      $parent_fields = TRUE;
      $form['terms'][$key]['term']['tid'] = array(
        '#type' => 'hidden',
        '#value' => $term->id(),
        '#attributes' => array(
          'class' => array('term-id'),
        ),
      );
      $form['terms'][$key]['term']['parent'] = array(
        '#type' => 'hidden',
        // Yes, default_value on a hidden. It needs to be changeable by the
        // javascript.
        '#default_value' => $term->parents[0],
        '#attributes' => array(
          'class' => array('term-parent'),
        ),
      );
      $form['terms'][$key]['term']['depth'] = array(
        '#type' => 'hidden',
        // Same as above, the depth is modified by javascript, so it's a
        // default_value.
        '#default_value' => $term->depth,
        '#attributes' => array(
          'class' => array('term-depth'),
        ),
      );
    }
    $form['terms'][$key]['weight'] = array(
      '#type' => 'weight',
      '#delta' => $delta,
      '#title' => $this->t('Weight for added term'),
      '#title_display' => 'invisible',
      '#default_value' => $term->getWeight(),
      '#attributes' => array(
        'class' => array('term-weight'),
      ),
    );
    $operations = array(
      'edit' => array(
        'title' => $this->t('Edit'),
        'query' => $destination,
        'url' => $term->urlInfo('edit-form'),
      ),
      'delete' => array(
        'title' => $this->t('Delete'),
        'query' => $destination,
        'url' => $term->urlInfo('delete-form'),
      ),
    );
    if ($this->moduleHandler->moduleExists('content_translation') && content_translation_translate_access($term)->isAllowed()) {
      $operations['translate'] = array(
        'title' => $this->t('Translate'),
        'query' => $destination,
        'url' => $term->urlInfo('drupal:content-translation-overview'),
      );
    }
    $form['terms'][$key]['operations'] = array(
      '#type' => 'operations',
      '#links' => $operations,
    );
 
    $form['terms'][$key]['#attributes']['class'] = array();
    if ($parent_fields) {
      $form['terms'][$key]['#attributes']['class'][] = 'draggable';
    }
 
    // Add classes that mark which terms belong to previous and next pages.
    if ($row_position < $back_step || $row_position >= $page_entries - $forward_step) {
      $form['terms'][$key]['#attributes']['class'][] = 'taxonomy-term-preview';
    }
 
    if ($row_position !== 0 && $row_position !== count($tree) - 1) {
      if ($row_position == $back_step - 1 || $row_position == $page_entries - $forward_step - 1) {
        $form['terms'][$key]['#attributes']['class'][] = 'taxonomy-term-divider-top';
      }
      elseif ($row_position == $back_step || $row_position == $page_entries - $forward_step) {
        $form['terms'][$key]['#attributes']['class'][] = 'taxonomy-term-divider-bottom';
      }
    }
 
    // Add an error class if this row contains a form error.
    foreach ($errors as $error_key => $error) {
      if (strpos($error_key, $key) === 0) {
        $form['terms'][$key]['#attributes']['class'][] = 'error';
      }
    }
    $row_position++;
  }
 
  if ($parent_fields) {
    $form['terms']['#tabledrag'][] = array(
      'action' => 'match',
      'relationship' => 'parent',
      'group' => 'term-parent',
      'subgroup' => 'term-parent',
      'source' => 'term-id',
      'hidden' => FALSE,
    );
    $form['terms']['#tabledrag'][] = array(
      'action' => 'depth',
      'relationship' => 'group',
      'group' => 'term-depth',
      'hidden' => FALSE,
    );
    $form['terms']['#attached']['library'][] = 'taxonomy/drupal.taxonomy';
    $form['terms']['#attached']['drupalSettings']['taxonomy'] = [
      'backStep' => $back_step,
      'forwardStep' => $forward_step,
    ];
  }
  $form['terms']['#tabledrag'][] = array(
    'action' => 'order',
    'relationship' => 'sibling',
    'group' => 'term-weight',
  );
 
  if ($taxonomy_vocabulary->getHierarchy() != VocabularyInterface::HIERARCHY_MULTIPLE && count($tree) > 1) {
    $form['actions'] = array('#type' => 'actions', '#tree' => FALSE);
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this->t('Save'),
      '#button_type' => 'primary',
    );
    $form['actions']['reset_alphabetical'] = array(
      '#type' => 'submit',
      '#submit' => array('::submitReset'),
      '#value' => $this->t('Reset to alphabetical'),
    );
  }
 
  $form['pager_pager'] = ['#type' => 'pager'];
  return $form;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.