diff --git a/core/modules/navigation/navigation.module b/core/modules/navigation/navigation.module index b3fd8c49382..b5907091fd6 100644 --- a/core/modules/navigation/navigation.module +++ b/core/modules/navigation/navigation.module @@ -42,6 +42,10 @@ function navigation_help($route_name, RouteMatchInterface $route_match) { * Implements hook_page_top(). */ function navigation_page_top(array &$page_top) { + if (!\Drupal::currentUser()->hasPermission('access navigation')) { + return; + } + $navigation_renderer = \Drupal::service('navigation.renderer'); assert($navigation_renderer instanceof NavigationRenderer); $navigation_renderer->removeToolbar($page_top); diff --git a/core/modules/navigation/navigation.services.yml b/core/modules/navigation/navigation.services.yml index cde62f21acb..a0f986aa149 100644 --- a/core/modules/navigation/navigation.services.yml +++ b/core/modules/navigation/navigation.services.yml @@ -7,7 +7,6 @@ services: '@module_handler', '@current_route_match', '@plugin.manager.menu.local_task', - '@current_user', '@entity_type.manager', '@image.factory', '@file_url_generator', diff --git a/core/modules/navigation/src/NavigationRenderer.php b/core/modules/navigation/src/NavigationRenderer.php index e8ad5913650..5f37f689a15 100644 --- a/core/modules/navigation/src/NavigationRenderer.php +++ b/core/modules/navigation/src/NavigationRenderer.php @@ -18,7 +18,6 @@ use Drupal\Core\Menu\LocalTaskManagerInterface; use Drupal\Core\Plugin\Context\Context; use Drupal\Core\Plugin\Context\ContextDefinition; use Drupal\Core\Routing\RouteMatchInterface; -use Drupal\Core\Session\AccountInterface; use Drupal\file\Entity\File; use Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface; use Symfony\Component\HttpFoundation\RequestStack; @@ -67,7 +66,6 @@ final class NavigationRenderer { private ModuleHandlerInterface $moduleHandler, private RouteMatchInterface $routeMatch, private LocalTaskManagerInterface $localTaskManager, - private AccountInterface $currentUser, private EntityTypeManagerInterface $entityTypeManager, private ImageFactory $imageFactory, private FileUrlGeneratorInterface $fileUrlGenerator, @@ -128,7 +126,6 @@ final class NavigationRenderer { $defaults = [ '#hide_logo' => $logo_provider === self::LOGO_PROVIDER_HIDE, - '#access' => $this->currentUser->hasPermission('access navigation'), '#attached' => [ 'html_head_link' => [ [ @@ -173,6 +170,10 @@ final class NavigationRenderer { * @see hook_page_top() */ public function buildTopBar(array &$page_top): void { + if (!$this->moduleHandler->moduleExists('navigation_top_bar')) { + return; + } + $page_top['top_bar'] = [ '#theme' => 'top_bar', '#attached' => [ @@ -186,13 +187,8 @@ final class NavigationRenderer { 'user.permissions', ], ], - '#access' => $this->currentUser->hasPermission('access navigation'), ]; - if (!$this->moduleHandler->moduleExists('navigation_top_bar')) { - return; - } - // Local tasks for content entities. if ($this->hasLocalTasks()) { $local_tasks = $this->getLocalTasks(); diff --git a/core/modules/navigation/tests/src/Functional/ShortcutsNavigationBlockTest.php b/core/modules/navigation/tests/src/Functional/ShortcutsNavigationBlockTest.php index fe771a9f6e8..1ae8307d9e0 100644 --- a/core/modules/navigation/tests/src/Functional/ShortcutsNavigationBlockTest.php +++ b/core/modules/navigation/tests/src/Functional/ShortcutsNavigationBlockTest.php @@ -118,14 +118,14 @@ class ShortcutsNavigationBlockTest extends PageCacheTagsTestBase { $this->drupalLogin($site_configuration_user1); $this->verifyDynamicPageCache($test_page_url, 'MISS'); $this->verifyDynamicPageCache($test_page_url, 'HIT'); - $this->assertCacheContexts(['user', 'url.query_args:_wrapper_format', 'url.path', 'session']); + $this->assertCacheContexts(['user', 'url.query_args:_wrapper_format', 'session']); $this->assertSession()->elementExists('css', '#menu--shortcuts'); $this->assertSession()->pageTextContains('Shortcuts'); $this->assertSession()->linkExists('Cron'); $this->drupalLogin($site_configuration_user2); $this->verifyDynamicPageCache($test_page_url, 'HIT'); - $this->assertCacheContexts(['user', 'url.query_args:_wrapper_format', 'url.path', 'session']); + $this->assertCacheContexts(['user', 'url.query_args:_wrapper_format', 'session']); $this->assertSession()->pageTextContains('Shortcuts'); $this->assertSession()->linkExists('Cron');