commit 1c21efa026408db381ec21ccf1ad4eb53cd78d53 Author: Bart Feenstra Date: Tue Feb 18 20:20:45 2014 +0100 foo diff --git a/core/includes/menu.inc b/core/includes/menu.inc index 7a79f5e..944353c 100644 --- a/core/includes/menu.inc +++ b/core/includes/menu.inc @@ -16,6 +16,7 @@ use Drupal\menu_link\MenuLinkStorageController; use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Routing\Exception\RouteNotFoundException; use Symfony\Component\Routing\Route; /** @@ -2554,11 +2555,44 @@ function _menu_link_save_recursive($controller, $machine_name, &$children, &$lin function menu_link_get_defaults() { $module_handler = \Drupal::moduleHandler(); $all_links = $module_handler->invokeAll('menu_link_defaults'); + /** @var \Drupal\Core\Routing\RouteProviderInterface $route_provider */ + $route_provider = \Drupal::service('router.route_provider'); + // Fill in the machine name from the array key. foreach ($all_links as $machine_name => &$link) { $link['machine_name'] = $machine_name; } + $module_handler->alter('menu_link_defaults', $all_links); + + // Validate the menu link definitions. + foreach ($all_links as $machine_name => $link) { + // Check for required array keys. + if (!isset($link['link_title'])) { + throw new \Exception(sprintf('Menu link %s does not have a "link_title" key in its definition.', $machine_name)); + } + + // Check if the specified parent menu link exists. + if (isset($link['parent']) && !isset($all_links[$link['parent']])) { + throw new \OutOfBoundsException(sprintf('Menu link %s specifies it has parent %s, but no menu link with that machine name exists.', $machine_name, $link['parent'])); + } + + // Check for either a route name or a link path. + if (!isset($link['route_name']) && !isset($link['link_path'])) { + throw new \Exception(sprintf('Menu link %s must provide either a "route_name" or a "link_path" key in its definition.', $machine_name)); + } + + // Check if the specified route exists. + if (isset($link['route_name'])) { + try { + $route_provider->getRouteByName($link['route_name']); + } + catch (RouteNotFoundException $e) { + throw new \Exception(sprintf('Menu link %s specifies route name %s, but no route with that machine name exists.', $machine_name, $link['route_name']), 0, $e); + } + } + } + return $all_links; } diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php index 2530db5..c3d2672 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -497,7 +497,7 @@ function hook_menu_get_item_alter(&$router_item, $path, $original_map) { * name, which must be unique. The corresponding array value is an * associative array that may contain the following key-value pairs: * - link_title: (required) The untranslated title of the menu item. - * - description: The untranslated description of the link. + * - description: (optional) The untranslated description of the link. * - route_name: (optional) The route name to be used to build the path. * Either a route_name or a link_path must be provided. * - route_parameters: (optional) The route parameters to build the path.