function menu_ui_get_menu_link_defaults in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/menu_ui/menu_ui.module \menu_ui_get_menu_link_defaults()
Returns the definition for a menu link for the given node.
Parameters
\Drupal\node\NodeInterface $node: The node entity.
Return value
array An array that contains default values for the menu link form.
1 call to menu_ui_get_menu_link_defaults()
- menu_ui_form_node_form_alter in core/
modules/ menu_ui/ menu_ui.module - Implements hook_form_BASE_FORM_ID_alter() for node_form.
File
- core/
modules/ menu_ui/ menu_ui.module, line 186 - Allows administrators to customize the site's navigation menus.
Code
function menu_ui_get_menu_link_defaults(NodeInterface $node) {
// Prepare the definition for the edit form.
/** @var \Drupal\node\NodeTypeInterface $node_type */
$node_type = $node->type->entity;
$menu_name = strtok($node_type
->getThirdPartySetting('menu_ui', 'parent', 'main:'), ':');
$defaults = FALSE;
if ($node
->id()) {
$id = FALSE;
// Give priority to the default menu
$type_menus = $node_type
->getThirdPartySetting('menu_ui', 'available_menus', array(
'main',
));
if (in_array($menu_name, $type_menus)) {
$query = \Drupal::entityQuery('menu_link_content')
->condition('link.uri', 'node/' . $node
->id())
->condition('menu_name', $menu_name)
->sort('id', 'ASC')
->range(0, 1);
$result = $query
->execute();
$id = !empty($result) ? reset($result) : FALSE;
}
// Check all allowed menus if a link does not exist in the default menu.
if (!$id && !empty($type_menus)) {
$query = \Drupal::entityQuery('menu_link_content')
->condition('link.uri', 'entity:node/' . $node
->id())
->condition('menu_name', array_values($type_menus), 'IN')
->sort('id', 'ASC')
->range(0, 1);
$result = $query
->execute();
$id = !empty($result) ? reset($result) : FALSE;
}
if ($id) {
$menu_link = MenuLinkContent::load($id);
$defaults = array(
'entity_id' => $menu_link
->id(),
'id' => $menu_link
->getPluginId(),
'title' => $menu_link
->getTitle(),
'description' => $menu_link
->getDescription(),
'menu_name' => $menu_link
->getMenuName(),
'parent' => $menu_link
->getParentId(),
'weight' => $menu_link
->getWeight(),
);
}
}
if (!$defaults) {
$defaults = array(
'entity_id' => 0,
'id' => '',
'title' => '',
'description' => '',
'menu_name' => $menu_name,
'parent' => '',
'weight' => 0,
);
}
return $defaults;
}