function MenuUiHooks::formNodeFormAlter
Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
Adds menu item fields to the node form.
See also
menu_ui_form_node_form_submit()
File
-
core/
modules/ menu_ui/ src/ Hook/ MenuUiHooks.php, line 92
Class
- MenuUiHooks
- Hook implementations for menu_ui.
Namespace
Drupal\menu_ui\HookCode
public function formNodeFormAlter(&$form, FormStateInterface $form_state) : void {
// Generate a list of possible parents (not including this link or descendants).
// @todo This must be handled in a #process handler.
$node = $form_state->getFormObject()
->getEntity();
$defaults = menu_ui_get_menu_link_defaults($node);
/** @var \Drupal\node\NodeTypeInterface $node_type */
$node_type = $node->type->entity;
/** @var \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector */
$menu_parent_selector = \Drupal::service('menu.parent_form_selector');
$type_menus_ids = $node_type->getThirdPartySetting('menu_ui', 'available_menus', [
'main',
]);
if (empty($type_menus_ids)) {
return;
}
/** @var \Drupal\system\MenuInterface[] $type_menus */
$type_menus = Menu::loadMultiple($type_menus_ids);
$available_menus = [];
foreach ($type_menus as $menu) {
$available_menus[$menu->id()] = $menu->label();
}
if ($defaults['id']) {
$default = $defaults['menu_name'] . ':' . $defaults['parent'];
}
else {
$default = $node_type->getThirdPartySetting('menu_ui', 'parent', 'main:');
}
$parent_element = $menu_parent_selector->parentSelectElement($default, $defaults['id'], $available_menus);
// If no possible parent menu items were found, there is nothing to display.
if (empty($parent_element)) {
return;
}
$form['menu'] = [
'#type' => 'details',
'#title' => t('Menu settings'),
'#access' => \Drupal::currentUser()->hasPermission('administer menu'),
'#open' => (bool) $defaults['id'],
'#group' => 'advanced',
'#attached' => [
'library' => [
'menu_ui/drupal.menu_ui',
],
],
'#tree' => TRUE,
'#weight' => -2,
'#attributes' => [
'class' => [
'menu-link-form',
],
],
];
$form['menu']['enabled'] = [
'#type' => 'checkbox',
'#title' => t('Provide a menu link'),
'#default_value' => (int) (bool) $defaults['id'],
];
$form['menu']['link'] = [
'#type' => 'container',
'#parents' => [
'menu',
],
'#states' => [
'invisible' => [
'input[name="menu[enabled]"]' => [
'checked' => FALSE,
],
],
],
];
// Populate the element with the link data.
foreach ([
'id',
'entity_id',
] as $key) {
$form['menu']['link'][$key] = [
'#type' => 'value',
'#value' => $defaults[$key],
];
}
$form['menu']['link']['title'] = [
'#type' => 'textfield',
'#title' => t('Menu link title'),
'#default_value' => $defaults['title'],
'#maxlength' => $defaults['title_max_length'],
];
$form['menu']['link']['description'] = [
'#type' => 'textfield',
'#title' => t('Description'),
'#default_value' => $defaults['description'],
'#description' => t('Shown when hovering over the menu link.'),
'#maxlength' => $defaults['description_max_length'],
];
$form['menu']['link']['menu_parent'] = $parent_element;
$form['menu']['link']['menu_parent']['#title'] = t('Parent link');
$form['menu']['link']['menu_parent']['#attributes']['class'][] = 'menu-parent-select';
$form['menu']['link']['weight'] = [
'#type' => 'number',
'#title' => t('Weight'),
'#default_value' => $defaults['weight'],
'#description' => t('Menu links with lower weights are displayed before links with higher weights.'),
];
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = 'menu_ui_form_node_form_submit';
}
}
$form['#entity_builders'][] = 'menu_ui_node_builder';
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.