Issue #3444040 by plopesc: Navigation render array is completely generated, even if the navigation bar is not rendered

(cherry picked from commit b33c9f935b)
merge-requests/7514/merge
catch 2024-04-29 18:30:00 +01:00
parent 993644f503
commit 2d0e8ae0f3
4 changed files with 10 additions and 11 deletions

View File

@ -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);

View File

@ -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',

View File

@ -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();

View File

@ -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');