diff --git a/core/includes/menu.inc b/core/includes/menu.inc index b678bb8..2dc24fb 100644 --- a/core/includes/menu.inc +++ b/core/includes/menu.inc @@ -2639,6 +2639,16 @@ function _menu_link_save_recursive($controller, $machine_name, &$children, &$lin } /** + * Find all links returned from hook_default_meanu_links(). + */ +function menu_get_default_links() { + $module_handler = \Drupal::moduleHandler(); + $all_links = $module_handler->invokeAll('default_menu_links'); + $module_handler->alter('default_menu_links', $all_links); + return $all_links; +} + +/** * Builds menu links for the items returned from hook_default_meanu_links(). */ function menu_default_links_rebuild() { @@ -2653,8 +2663,7 @@ function menu_default_links_rebuild() { $links = array(); $children = array(); $top_links = array(); - $all_links = $module_handler->invokeAll('default_menu_links'); - $module_handler->alter('default_menu_links', $all_links); + $all_links = menu_get_default_links(); if ($all_links) { foreach ($all_links as $machine_name => $link) { // Note, we set this as 'system', so that we can be sure to distinguish all diff --git a/core/modules/menu_link/lib/Drupal/menu_link/Entity/MenuLink.php b/core/modules/menu_link/lib/Drupal/menu_link/Entity/MenuLink.php index 527ba57..5fc83ce 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/Entity/MenuLink.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/Entity/MenuLink.php @@ -375,14 +375,15 @@ public function setRouteObject(Route $route) { */ public function reset() { // To reset the link to its original values, we need to retrieve its - // definition from hook_menu(). Otherwise, for example, the link's menu - // would not be reset, because properties like the original 'menu_name' are - // not stored anywhere else. Since resetting a link happens rarely and this - // is a one-time operation, retrieving the full menu router does no harm. + // definition from hook_default_menu_links(). Otherwise, for example, the + // link's menu would not be reset, because properties like the original + // 'menu_name' are not stored anywhere else. Since resetting a link happens + // rarely and this is a one-time operation, retrieving the full menu router + // does little harm. // @FIXME Decide whether we want to keep the reset functionality. - $menu = menu_get_router(); - $router_item = $menu[$this->link_path]; - $new_link = self::buildFromRouterItem($router_item); + $all_links = menu_get_default_links(); + $original = $all_links[$this->machine_name]; + $new_link = $this->buildFromOriginalItem($original); // Merge existing menu link's ID and 'has_children' property. foreach (array('mlid', 'has_children') as $key) { $new_link->{$key} = $this->{$key}; @@ -392,6 +393,26 @@ public function reset() { } /** + * Create an entity from a default menu link item. + */ + protected function buildFromOriginalItem(array $item) { + // Suggested items are disabled by default. + $item += array( + 'type' => MENU_NORMAL_ITEM, + 'hidden' => 0, + 'options' => empty($item['description']) ? array() : array('attributes' => array('title' => $item['description'])), + ); + if ($item['type'] == MENU_SUGGESTED_ITEM) { + $item['hidden'] = 1; + } + // Note, we set this as 'system', so that we can be sure to distinguish all + // the menu links generated automatically from entries in {menu_router}. + $item['module'] = 'system'; + return \Drupal::entityManager() + ->getStorageController('menu_link')->create($item); + } + + /** * Implements ArrayAccess::offsetExists(). */ public function offsetExists($offset) { diff --git a/core/modules/system/lib/Drupal/system/Tests/Menu/LinksTest.php b/core/modules/system/lib/Drupal/system/Tests/Menu/LinksTest.php index bcf7c96..c3da82b 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Menu/LinksTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Menu/LinksTest.php @@ -183,7 +183,7 @@ function testMenuLinkReparenting($module = 'menu_test') { } /** - * Test automatic reparenting of menu links derived from menu routers. + * Test automatic reparenting of menu links derived from hook_default_men_links. */ function testMenuLinkRouterReparenting() { // Run all the standard parenting tests on menu links derived from @@ -224,8 +224,7 @@ function testMenuLinkRouterReparenting() { $this->assertMenuLinkParents($links, $expected_hierarchy); // Now delete 'child-2' directly from the database, simulating a database - // crash. 'child-1-2' will get reparented under 'child-1' based on its - // path. + // crash. 'child-1-2' will get reparented to the top. // Don't do that at home. db_delete('menu_links') ->condition('mlid', $links['child-2']['mlid']) @@ -233,7 +232,7 @@ function testMenuLinkRouterReparenting() { $expected_hierarchy = array( 'child-1' => FALSE, 'child-1-1' => 'child-1', - 'child-1-2' => 'child-1', + 'child-1-2' => FALSE, ); $this->assertMenuLinkParents($links, $expected_hierarchy); }