diff -u b/core/includes/menu.inc b/core/includes/menu.inc --- b/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,21 +2555,44 @@ 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)); - } + 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. + // 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); + } + } } - $module_handler->alter('menu_link_defaults', $all_links); + return $all_links; }