protected function MenuForm::buildOverviewForm in Drupal 9
Same name and namespace in other branches
- 8 core/modules/menu_ui/src/MenuForm.php \Drupal\menu_ui\MenuForm::buildOverviewForm()
Form constructor to edit an entire menu tree at once.
Shows for one menu the menu links accessible to the current user and relevant operations.
This form constructor can be integrated as a section into another form. It relies on the following keys in $form_state:
- menu: A menu entity.
- menu_overview_form_parents: An array containing the parent keys to this form.
Forms integrating this section should call menu_overview_form_submit() from their form submit handler.
1 call to MenuForm::buildOverviewForm()
- MenuForm::form in core/
modules/ menu_ui/ src/ MenuForm.php - Gets the actual form array to be built.
File
- core/
modules/ menu_ui/ src/ MenuForm.php, line 219
Class
- MenuForm
- Base form for menu edit forms.
Namespace
Drupal\menu_uiCode
protected function buildOverviewForm(array &$form, FormStateInterface $form_state) {
// 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', []);
}
$form['#attached']['library'][] = 'menu_ui/drupal.menu_ui.adminforms';
$tree = $this->menuTree
->load($this->entity
->id(), new MenuTreeParameters());
// 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);
// 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);
};
$delta = max($count($tree), 50);
$form['links'] = [
'#type' => 'table',
'#theme' => 'table__menu_overview',
'#header' => [
$this
->t('Menu link'),
[
'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',
],
],
];
$form['links']['#empty'] = $this
->t('There are no menu links yet. <a href=":url">Add link</a>.', [
':url' => Url::fromRoute('entity.menu.add_link_form', [
'menu' => $this->entity
->id(),
], [
'query' => [
'destination' => $this->entity
->toUrl('edit-form')
->toString(),
],
])
->toString(),
]);
$links = $this
->buildOverviewTreeForm($tree, $delta);
// Get the menu links which have pending revisions, and disable the
// tabledrag if there are any.
$edited_ids = array_filter(array_map(function ($element) {
return is_array($element) && isset($element['#item']) && $element['#item']->link instanceof MenuLinkContent ? $element['#item']->link
->getMetaData()['entity_id'] : NULL;
}, $links));
$pending_menu_link_ids = array_intersect($this->menuLinkContentStorage
->getMenuLinkIdsWithPendingRevisions(), $edited_ids);
if ($pending_menu_link_ids) {
$form['help'] = [
'#type' => 'container',
'message' => [
'#markup' => $this
->formatPlural(count($pending_menu_link_ids), '%capital_name contains 1 menu link with pending revisions. Manipulation of a menu tree having links with pending revisions is not supported, but you can re-enable manipulation by getting each menu link to a published state.', '%capital_name contains @count menu links with pending revisions. Manipulation of a menu tree having links with pending revisions is not supported, but you can re-enable manipulation by getting each menu link to a published state.', [
'%capital_name' => $this->entity
->label(),
]),
],
'#attributes' => [
'class' => [
'messages',
'messages--warning',
],
],
'#weight' => -10,
];
unset($form['links']['#tabledrag']);
unset($form['links']['#header'][2]);
}
foreach (Element::children($links) as $id) {
if (isset($links[$id]['#item'])) {
$element = $links[$id];
$is_pending_menu_link = isset($element['#item']->link
->getMetaData()['entity_id']) && in_array($element['#item']->link
->getMetaData()['entity_id'], $pending_menu_link_ids);
$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';
if ($is_pending_menu_link) {
$form['links'][$id]['#attributes']['class'][] = 'color-warning';
$form['links'][$id]['#attributes']['class'][] = 'menu-link-content--pending-revision';
}
// 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]['enabled'] = $element['enabled'];
$form['links'][$id]['enabled']['#wrapper_attributes']['class'] = [
'checkbox',
'menu-enabled',
];
// Disallow changing the publishing status of a pending revision.
if ($is_pending_menu_link) {
$form['links'][$id]['enabled']['#access'] = FALSE;
}
if (!$pending_menu_link_ids) {
$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'];
}
}
return $form;
}