diff --git a/core/modules/menu_link/menu_link.api.php b/core/modules/menu_link/menu_link.api.php index bfb8a2e462aa0f4c3096e50ac450a2aff7edf86f..197aee8dfa89264c305797f02ce9b4f142b3f996 100644 --- a/core/modules/menu_link/menu_link.api.php +++ b/core/modules/menu_link/menu_link.api.php @@ -10,6 +10,36 @@ * @{ */ +/** + * Alter a menu link after it has been translated and before it is rendered. + * + * This hook is invoked from _menu_link_translate() after a menu link has been + * translated; i.e., after dynamic path argument placeholders (%) have been + * replaced with actual values, the user access to the link's target page has + * been checked, and the link has been localized. It is only invoked if + * $menu_link['options']['alter'] has been set to a non-empty value (e.g. TRUE). + * This flag should be set using hook_menu_link_presave(). + * + * Implementations of this hook are able to alter any property of the menu link. + * For example, this hook may be used to add a page-specific query string to all + * menu links, or hide a certain link by setting: + * @code + * 'hidden' => 1, + * @endcode + * + * @param \Drupal\menu_link\Entity\MenuLink $menu_link + * A menu link entity. + * @param array $map + * Associative array containing the menu $map (path parts and/or objects). + * + * @see hook_menu_link_alter() + */ +function hook_translated_menu_link_alter(\Drupal\menu_link\Entity\MenuLink &$menu_link, $map) { + if ($menu_link->href == 'devel/cache/clear') { + $menu_link->localized_options['query'] = drupal_get_destination(); + } +} + /** * Alter menu links when loaded and before they are rendered. * diff --git a/core/modules/user/lib/Drupal/user/Tests/UserAccountLinksTests.php b/core/modules/user/lib/Drupal/user/Tests/UserAccountLinksTests.php index 865878af83d9f5438708633270e405ace66a009a..24e7244623fbc8af5c9ec92f9cc2b5827e9df343 100644 --- a/core/modules/user/lib/Drupal/user/Tests/UserAccountLinksTests.php +++ b/core/modules/user/lib/Drupal/user/Tests/UserAccountLinksTests.php @@ -19,7 +19,7 @@ class UserAccountLinksTests extends WebTestBase { * * @var array */ - public static $modules = array('menu', 'block'); + public static $modules = array('menu', 'block', 'test_page_test'); public static function getInfo() { return array( @@ -29,6 +29,12 @@ public static function getInfo() { ); } + public function setUp() { + parent::setUp(); + // Make test-page default. + \Drupal::config('system.site')->set('page.front', 'test-page')->save(); + } + /** * Tests the secondary menu. */ @@ -61,8 +67,11 @@ function testSecondaryMenu() { $this->drupalGet('<front>'); // For a logged-out user, expect no secondary links. - $element = $this->xpath('//ul[@id=:menu_id]', array(':menu_id' => 'secondary-menu')); - $this->assertEqual(count($element), 0, 'No secondary-menu for logged-out users.'); + $tree = menu_build_tree('account'); + $this->assertEqual(count($tree), 1, 'The secondary links menu contains only one menu link.'); + $link = reset($tree); + $link = $link['link']; + $this->assertTrue((bool) $link->hidden, 'The menu link is hidden.'); } /** diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 8dfae6ad037a4afcd2e91f28fca7f99447757863..b55cee6cf5b6d50cce8e293368a30ee15d2fc48b 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -854,14 +854,12 @@ function user_menu_breadcrumb_alter(&$active_trail, $item) { } /** - * Implements hook_menu_link_load(). + * Implements hook_translated_menu_link_alter(). */ -function user_menu_link_load($menu_links) { +function user_translated_menu_link_alter(MenuLink &$menu_link) { // Hide the "User account" link for anonymous users. - foreach ($menu_links as $link) { - if ($link['link_path'] == 'user' && $link['module'] == 'system' && !$GLOBALS['user']->id()) { - $link['hidden'] = 1; - } + if ($menu_link->link_path == 'user' && $menu_link->module == 'system' && \Drupal::currentUser()->isAnonymous()) { + $menu_link->hidden = 1; } }