Newer
Older
Brian Canini
committed
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
<?php
namespace Drupal\bigmenu;
use Drupal\Core\Url;
use Drupal\menu_ui\MenuForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuLinkTreeElement;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Render\Element;
/**
* Defines class for BigMenuForm.
*/
class BigMenuForm extends MenuForm {
/**
* The menu tree.
*
* @var array
*/
protected $tree = [];
/**
* Overrides Drupal\menu_ui\MenuForm::buildOverviewForm() to limit the depth.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
*
* @return array
* The form.
*/
protected function buildOverviewForm(array &$form, FormStateInterface $form_state) {
$menu_link = $this->getRequest()->query->get('menu_link');
$form['#cache']['contexts'][] = 'url.query_args:menu_link';
return $this->buildOverviewFormWithDepth($form, $form_state, 1, $menu_link);
}
/**
* Build a shallow version of the overview form.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
* @param int $depth
* The depth.
* @param string $menu_link
* (Optional) The starting menu link id.
*
* @return array
* The form.
*/
protected function buildOverviewFormWithDepth(array &$form, FormStateInterface $form_state, $depth = 1, $menu_link = NULL) {
// Ensure that menu_overview_form_submit() knows the parents of this form
// section.
if (!$form_state->has('menu_overview_form_parents')) {
$form_state->set('menu_overview_form_parents', []);
}
// Use Menu UI adminforms.
$form['#attached']['library'][] = 'menu_ui/drupal.menu_ui.adminforms';
// Add a link to go back to the full menu.
if ($menu_link) {
$form['back_link'] = $this->entity->toLink($this->t('Back to @label top level', [
'@label' => $this->entity->label(),
]), 'edit-form')->toRenderable();
}
$form['links'] = [
'#type' => 'table',
'#theme' => 'table__menu_overview',
'#header' => [
$this->t('Menu link'),
$this->t('Edit children'),
[
'data' => $this->t('Enabled'),
'class' => ['checkbox'],
],
$this->t('Weight'),
[
'data' => $this->t('Operations'),
'colspan' => 3,
],
],
'#attributes' => [
'id' => 'menu-overview',
],
'#tabledrag' => [
[
'action' => 'match',
'relationship' => 'parent',
'group' => 'menu-parent',
'subgroup' => 'menu-parent',
'source' => 'menu-id',
'hidden' => TRUE,
'limit' => $this->menuTree->maxDepth() - 1,
],
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'menu-weight',
],
],
];
// No Links available (Empty menu)
$form['links']['#empty'] = $this->t('There are no menu links yet. <a href=":url">Add link</a>.', [
':url' => $this->entity->toUrl('add-link-form', [
'query' => ['destination' => $this->entity->toUrl('edit-form')->toString()],
])->toString(),
]);
// Get the menu tree if it's not in our property.
if (empty($this->tree)) {
$this->tree = $this->getTree($depth, $menu_link);
}
// Determine the delta; the number of weights to be made available.
$count = function (array $tree) {
$sum = function ($carry, MenuLinkTreeElement $item) {
return $carry + $item->count();
};
return array_reduce($tree, $sum);
};
// Tree maximum or 50.
$delta = max($count($this->tree), 50);
$links = $this->buildOverviewTreeForm($this->tree, $delta);
$this->processLinks($form, $links, $menu_link);
return $form;
}
/**
* Format the links appropriately so draggable views will work.
*
* @param array $form
* The form array.
* @param array $links
* An array of links.
* @param string $menu_link
* A menu link plugin id.
*/
public function processLinks(array &$form, array &$links, $menu_link) {
foreach (Element::children($links) as $id) {
if (isset($links[$id]['#item'])) {
$element = $links[$id];
$form['links'][$id]['#item'] = $element['#item'];
// TableDrag: Mark the table row as draggable.
$form['links'][$id]['#attributes'] = $element['#attributes'];
$form['links'][$id]['#attributes']['class'][] = 'draggable';
// TableDrag: Sort the table row according to its existing/configured
// weight.
$form['links'][$id]['#weight'] = $element['#item']->link->getWeight();
// Add special classes to be used for tabledrag.js.
$element['parent']['#attributes']['class'] = ['menu-parent'];
$element['weight']['#attributes']['class'] = ['menu-weight'];
$element['id']['#attributes']['class'] = ['menu-id'];
$form['links'][$id]['title'] = [
[
'#theme' => 'indentation',
'#size' => $element['#item']->depth - 1,
],
$element['title'],
];
$form['links'][$id]['root'][] = [];
if ($form['links'][$id]['#item']->hasChildren) {
if (is_null($menu_link) || (isset($menu_link) && $menu_link != $element['#item']->link->getPluginId())) {
$uri = $this->entity->toUrl('edit-form', [
'query' => ['menu_link' => $element['#item']->link->getPluginId()],
]);
$form['links'][$id]['root'][] = [
'#type' => 'link',
'#title' => $this->t('Edit child items'),
'#url' => $uri,
];
}
}
$form['links'][$id]['enabled'] = $element['enabled'];
$form['links'][$id]['enabled']['#wrapper_attributes']['class'] = ['checkbox', 'menu-enabled'];
$form['links'][$id]['weight'] = $element['weight'];
// Operations (dropbutton) column.
$form['links'][$id]['operations'] = $element['operations'];
$form['links'][$id]['id'] = $element['id'];
$form['links'][$id]['parent'] = $element['parent'];
}
}
}
/**
* Gets the menu tree.
*
* @param int $depth
* The depth.
* @param string $root
* An optional root menu link plugin id.
*
* @return \Drupal\Core\Menu\MenuLinkTreeElement[]
* An array of menu link tree elements.
*/
protected function getTree($depth, $root = NULL) {
$tree_params = new MenuTreeParameters();
$tree_params->setMaxDepth($depth);
if ($root) {
$tree_params->setRoot($root);
}
$tree = $this->menuTree->load($this->entity->id(), $tree_params);
// We indicate that a menu administrator is running the menu access check.
$this->getRequest()->attributes->set('_menu_admin', TRUE);
$manipulators = [
['callable' => 'menu.default_tree_manipulators:checkAccess'],
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
];
$tree = $this->menuTree->transform($tree, $manipulators);
$this->getRequest()->attributes->set('_menu_admin', FALSE);
return $tree;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
parent::save($form, $form_state);
$form_state->setRedirectUrl(Url::fromUserInput($this->getRedirectDestination()->get()));
}
}