function MenuUiUtility::getMenuLinkDefaults
Same name and namespace in other branches
- 11.x core/modules/menu_ui/src/MenuUiUtility.php \Drupal\menu_ui\MenuUiUtility::getMenuLinkDefaults()
Returns the definition for a menu link for the given node.
Parameters
\Drupal\node\NodeInterface $node: The node entity.
Return value
array|bool An array that contains default values for the menu link form. FALSE as a fallback.
File
-
core/
modules/ menu_ui/ src/ MenuUiUtility.php, line 98
Class
- MenuUiUtility
- Utility functions for menu_ui.
Namespace
Drupal\menu_uiCode
public function getMenuLinkDefaults(NodeInterface $node) : false|array {
// 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', [
'main',
]);
// An existing menu link either points to the canonical or the latest
// path, in case of a new menu link that was creating while saving as a
// pending draft.
$uri_candidates = [
'entity:node/' . $node->id(),
'internal:/node/' . $node->id() . '/latest',
];
if (in_array($menu_name, $type_menus)) {
$query = $this->entityTypeManager
->getStorage('menu_link_content')
->getQuery()
->accessCheck()
->condition('link.uri', $uri_candidates, 'IN')
->condition('menu_name', $menu_name)
->sort('id')
->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 = $this->entityTypeManager
->getStorage('menu_link_content')
->getQuery()
->accessCheck()
->condition('link.uri', $uri_candidates, 'IN')
->condition('menu_name', array_values($type_menus), 'IN')
->sort('id')
->range(0, 1);
$result = $query->execute();
$id = !empty($result) ? reset($result) : FALSE;
}
if ($id) {
$menu_link = $this->entityRepository
->getActive('menu_link_content', $id);
$defaults = [
'entity_id' => $menu_link->id(),
'id' => $menu_link->getPluginId(),
'title' => $menu_link->getTitle(),
'title_max_length' => $menu_link->getFieldDefinitions()['title']
->getSetting('max_length'),
'description' => $menu_link->getDescription(),
'description_max_length' => $menu_link->getFieldDefinitions()['description']
->getSetting('max_length'),
'menu_name' => $menu_link->getMenuName(),
'parent' => $menu_link->getParentId(),
'weight' => $menu_link->getWeight(),
];
}
}
if (!$defaults) {
// Get the default max_length of a menu link title from the base field
// definition.
$field_definitions = $this->entityFieldManager
->getBaseFieldDefinitions('menu_link_content');
$max_length = $field_definitions['title']->getSetting('max_length');
$description_max_length = $field_definitions['description']->getSetting('max_length');
$defaults = [
'entity_id' => 0,
'id' => '',
'title' => '',
'title_max_length' => $max_length,
'description' => '',
'description_max_length' => $description_max_length,
'menu_name' => $menu_name,
'parent' => '',
'weight' => 0,
];
}
return $defaults;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.