menu_ui_form_node_form_alter(&$form, FormStateInterface $form_state)
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/menu_ui.module, line 266
- Allows administrators to customize the site's navigation menus.
Code
function menu_ui_form_node_form_alter(&$form, FormStateInterface $form_state) { // 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'); $menu_names = menu_ui_get_menus(); $type_menus = $node_type->getThirdPartySetting('menu_ui', 'available_menus', array('main')); $available_menus = array(); foreach ($type_menus as $menu) { $available_menus[$menu] = $menu_names[$menu]; } 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'] = array( '#type' => 'details', '#title' => t('Menu settings'), '#access' => \Drupal::currentUser()->hasPermission('administer menu'), '#open' => (bool) $defaults['id'], '#group' => 'advanced', '#attached' => array( 'library' => array('menu_ui/drupal.menu_ui'), ), '#tree' => TRUE, '#weight' => -2, '#attributes' => array('class' => array('menu-link-form')), ); $form['menu']['enabled'] = array( '#type' => 'checkbox', '#title' => t('Provide a menu link'), '#default_value' => (int) (bool) $defaults['id'], ); $form['menu']['link'] = array( '#type' => 'container', '#parents' => array('menu'), '#states' => array( 'invisible' => array( 'input[name="menu[enabled]"]' => array('checked' => FALSE), ), ), ); // Populate the element with the link data. foreach (array('id', 'entity_id') as $key) { $form['menu']['link'][$key] = array('#type' => 'value', '#value' => $defaults[$key]); } $form['menu']['link']['title'] = array( '#type' => 'textfield', '#title' => t('Menu link title'), '#default_value' => $defaults['title'], '#maxlength' => $defaults['title_max_length'], ); $form['menu']['link']['description'] = array( '#type' => 'textarea', '#title' => t('Description'), '#default_value' => $defaults['description'], '#rows' => 1, '#description' => t('Shown when hovering over the menu link.'), ); $form['menu']['link']['menu_parent'] = $parent_element; $form['menu']['link']['menu_parent']['#title'] = t('Parent item'); $form['menu']['link']['menu_parent']['#attributes']['class'][] = 'menu-parent-select'; $form['menu']['link']['weight'] = array( '#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'; } } }
Please login to continue.