diff --git a/core/includes/menu.inc b/core/includes/menu.inc index 4b83d7c..15708f8 100644 --- a/core/includes/menu.inc +++ b/core/includes/menu.inc @@ -36,9 +36,9 @@ * the stuff in the routing.yml file means. * * @section Defining menu links - * Once you have a route defined, you can use hook_menu_link_defaults() to + * Once you have a route defined, you can use module.menu_links.yml to * define links for your module's paths in the main Navigation menu or other - * menus. See the hook_menu_link_defaults() documentation for more details. + * menus. * * @todo The rest of this topic has not been reviewed or updated for Drupal 8.x * and is not correct! @@ -917,7 +917,7 @@ function _menu_link_save_recursive($controller, $machine_name, &$children, &$lin } /** - * Builds menu links for the items returned from hook_menu_link_defaults(). + * Builds menu links for the items returned from the menu_link.static service. */ function menu_link_rebuild_defaults() { // Ensure that all configuration used to build the menu items are loaded @@ -961,7 +961,7 @@ function menu_link_rebuild_defaults() { $existing_item = $menu_link_storage->create(get_object_vars($existing_item)); if (!$existing_item->customized) { - // A change in hook_menu_link_defaults() may move the link to a + // A change in the default menu links may move the link to a // different menu or parent. if (!empty($link['menu_name']) && ($link['menu_name'] != $existing_item->menu_name)) { $menu_link->plid = NULL; @@ -1009,7 +1009,7 @@ function menu_link_rebuild_defaults() { } } - // Find any item whose entry in hook_menu_link_defaults() no longer exists. + // Find any item whose default menu link no longer exists. if ($all_links) { $query = \Drupal::entityQuery('menu_link') ->condition('machine_name', array_keys($all_links), 'NOT IN') diff --git a/core/modules/menu/lib/Drupal/menu/Form/MenuDeleteForm.php b/core/modules/menu/lib/Drupal/menu/Form/MenuDeleteForm.php index 31576df..bcd5611 100644 --- a/core/modules/menu/lib/Drupal/menu/Form/MenuDeleteForm.php +++ b/core/modules/menu/lib/Drupal/menu/Form/MenuDeleteForm.php @@ -104,10 +104,11 @@ public function submit(array $form, array &$form_state) { return; } - // Reset all the menu links defined by the system via hook_menu_link_defaults(). + // Reset all the menu links defined by the menu_link.static service. $result = \Drupal::entityQuery('menu_link') ->condition('menu_name', $this->entity->id()) - ->condition('module', 'system') + ->condition('module', '', '>') + ->condition('machine_name', '', '>') ->sort('depth', 'ASC') ->execute(); $menu_links = $this->storage->loadMultiple($result); 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 0e9ba4d..65356a6 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 @@ -64,7 +64,7 @@ class MenuLink extends Entity implements \ArrayAccess, MenuLinkInterface { public $mlid; /** - * An optional machine name if defined via hook_menu_link_defaults(). + * An optional machine name if defined via the menu_link.static service. * * @var string */ @@ -358,8 +358,8 @@ 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_link_defaults(). Otherwise, for example, the - // link's menu would not be reset, because properties like the original + // definition from the menu_link.static service.. 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 set of // default menu links does little harm. @@ -369,6 +369,21 @@ public function reset() { /** @var \Drupal\menu_link\MenuLinkStorageInterface $storage */ $storage = \Drupal::entityManager()->getStorage($this->entityTypeId); $new_link = $storage->createFromDefaultLink($original); + // Allow the menu to be determined by the parent + if (!empty($new_link['parent']) && !empty($all_links[$new_link['parent']])) { + // Walk up the tree to find the menu name. + $parent = $all_links[$new_link['parent']]; + $existing_parent = db_select('menu_links') + ->fields('menu_links') + ->condition('machine_name', $parent['machine_name']) + ->execute()->fetchObject(); + if ($existing_parent) { + /** @var \Drupal\menu_link\MenuLinkInterface $existing_item */ + $existing_parent = $storage->create(get_object_vars($existing_parent)); + $new_link->menu_name = $existing_parent->menu_name; + $new_link->plid = $existing_parent->id(); + } + } // Merge existing menu link's ID and 'has_children' property. foreach (array('mlid', 'has_children') as $key) { $new_link->{$key} = $this->{$key}; @@ -592,7 +607,7 @@ protected function setParents(MenuLinkInterface $parent) { protected function findParent(EntityStorageInterface $storage) { $parent = FALSE; - // This item is explicitely top-level, skip the rest of the parenting. + // This item is explicitly top-level, skip the rest of the parenting. if (isset($this->plid) && empty($this->plid)) { return $parent; } diff --git a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageInterface.php b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageInterface.php index 4a9d719..3d3fd5d 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageInterface.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageInterface.php @@ -97,10 +97,10 @@ public function getParentFromHierarchy(EntityInterface $entity); * Builds a menu link entity from a default item. * * This function should only be called for link data from - * hook_menu_link_defaults(). + * the menu_link.static service. * * @param array $item - * An item returned from menu_links_get_defaults(). + * An item returned from the menu_link.static service. * * @return \Drupal\menu_link\MenuLinkInterface * A menu link entity. diff --git a/core/modules/menu_link/lib/Drupal/menu_link/StaticMenuLinks.php b/core/modules/menu_link/lib/Drupal/menu_link/StaticMenuLinks.php index ceae2e9..766c919 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/StaticMenuLinks.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/StaticMenuLinks.php @@ -10,7 +10,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface; /** - * Provides you a service which are defined in yml files and alter them. + * Provides a service which finds default menu links in yml files and alters them. */ class StaticMenuLinks { 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 4d7a735..2bfe779 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Menu/LinksTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Menu/LinksTest.php @@ -182,7 +182,7 @@ function testMenuLinkReparenting($module = 'menu_test') { } /** - * Tests automatic reparenting of menu links derived from hook_menu_link_defaults. + * Tests automatic reparenting of menu links defined by the menu_link.static service. */ function testMenuLinkRouterReparenting() { // Run all the standard parenting tests on menu links derived from diff --git a/core/modules/system/lib/Drupal/system/Tests/Menu/MenuRouterTest.php b/core/modules/system/lib/Drupal/system/Tests/Menu/MenuRouterTest.php index 822dddd..03f8ca1 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Menu/MenuRouterTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Menu/MenuRouterTest.php @@ -10,7 +10,7 @@ use Drupal\simpletest\WebTestBase; /** - * Tests menu router and hook_menu_link_defaults() functionality. + * Tests menu router and default menu link functionality. */ class MenuRouterTest extends WebTestBase { @@ -45,7 +45,7 @@ class MenuRouterTest extends WebTestBase { public static function getInfo() { return array( 'name' => 'Menu router', - 'description' => 'Tests menu router and hook_menu_link_defaults() functionality.', + 'description' => 'Tests menu router and default menu links functionality.', 'group' => 'Menu', ); } @@ -165,7 +165,7 @@ protected function doTestMenuLinkMaintain() { } /** - * Tests for menu_name parameter for hook_menu_link_defaults(). + * Tests for menu_name parameter for default menu links. */ protected function doTestMenuName() { $admin_user = $this->drupalCreateUser(array('administer site configuration')); diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 042a920..7b35fca 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -1469,15 +1469,15 @@ function system_admin_compact_mode() { /** * Generate a list of tasks offered by a specified module. * - * @param $module + * @param string $module * Module name. - * @param $info + * @param array $info * The module's information, as provided by system_get_info(). * - * @return + * @return array * An array of task links. */ -function system_get_module_admin_tasks($module, $info) { +function system_get_module_admin_tasks($module, array $info) { $links = &drupal_static(__FUNCTION__); if (!isset($links)) { @@ -1500,8 +1500,8 @@ function system_get_module_admin_tasks($module, $info) { $machine_name = $item['machine_name']; if (isset($links[$machine_name])) { $task = $links[$machine_name]; - // The link description, either derived from 'description' in - // hook_menu() or customized via menu module is used as title attribute. + // The link description, either derived from 'description' in the default + // menu link or customized via menu module is used as title attribute. if (!empty($task['localized_options']['attributes']['title'])) { $task['description'] = $task['localized_options']['attributes']['title']; unset($task['localized_options']['attributes']['title']); diff --git a/core/modules/system/tests/modules/menu_test/menu_test.module b/core/modules/system/tests/modules/menu_test/menu_test.module index 69b758a..9e6f351 100644 --- a/core/modules/system/tests/modules/menu_test/menu_test.module +++ b/core/modules/system/tests/modules/menu_test/menu_test.module @@ -16,7 +16,7 @@ */ function menu_test_menu_link_defaults_alter(&$links) { $links['menu_test.menu_name_test']['menu_name'] = menu_test_menu_name(); - $links['menu_test.context']['link_title'] = \Drupal::config('menu_test.menu_item')->get('title'); + $links['menu_test.context']['title'] = \Drupal::config('menu_test.menu_item')->get('title'); } /**