diff --git a/core/modules/navigation/navigation.module b/core/modules/navigation/navigation.module index 78b36d83022b4065bce3d0601af3412b15cdd899..b3fd8c4938202f0c7ab4da13b8fc8aab6c38769c 100644 --- a/core/modules/navigation/navigation.module +++ b/core/modules/navigation/navigation.module @@ -148,22 +148,6 @@ function navigation_block_build_local_tasks_block_alter(array &$build, BlockPlug $navigation_renderer->removeLocalTasks($build, $block); } -/** - * Implements hook_preprocess_html(). - */ -function navigation_preprocess_html(&$variables) { - $module_path = \Drupal::request()->getBasePath() . '/' . \Drupal::service('extension.list.module')->getPath('navigation'); - $asset_url = $module_path . '/assets/fonts/inter-var.woff2'; - $variables['#attached']['html_head_link'][] = [ - [ - 'rel' => 'preload', - 'href' => $asset_url, - 'as' => 'font', - 'crossorigin' => 'anonymous', - ], - ]; -} - /** * Implements hook_plugin_filter_TYPE__CONSUMER_alter(). * diff --git a/core/modules/navigation/navigation.services.yml b/core/modules/navigation/navigation.services.yml index 5d9aac7734164e4a1311cc96637aff57bdffcbc7..cde62f21acbd0fbb34da5e02df789c95f71bf5db 100644 --- a/core/modules/navigation/navigation.services.yml +++ b/core/modules/navigation/navigation.services.yml @@ -12,6 +12,8 @@ services: '@image.factory', '@file_url_generator', '@plugin.manager.layout_builder.section_storage', + '@request_stack', + '@extension.list.module' ] Drupal\navigation\NavigationRenderer: '@navigation.renderer' diff --git a/core/modules/navigation/src/NavigationRenderer.php b/core/modules/navigation/src/NavigationRenderer.php index 263f1dc7bfd8f4d62f87d097249796a71fea9204..e8ad591365092fd42de5a64a15774a5a82f2cada 100644 --- a/core/modules/navigation/src/NavigationRenderer.php +++ b/core/modules/navigation/src/NavigationRenderer.php @@ -2,6 +2,7 @@ namespace Drupal\navigation; +use Drupal\Component\Utility\NestedArray; use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Config\ConfigFactoryInterface; @@ -9,6 +10,7 @@ use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\File\FileUrlGeneratorInterface; use Drupal\Core\Image\ImageFactory; @@ -19,6 +21,7 @@ use Drupal\Core\Session\AccountInterface; use Drupal\file\Entity\File; use Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface; +use Symfony\Component\HttpFoundation\RequestStack; /** * Handle rendering for different pieces of the navigation. @@ -69,6 +72,8 @@ public function __construct( private ImageFactory $imageFactory, private FileUrlGeneratorInterface $fileUrlGenerator, private SectionStorageManagerInterface $sectionStorageManager, + private RequestStack $requestStack, + private ModuleExtensionList $moduleExtensionList, ) {} /** @@ -118,10 +123,26 @@ public function buildNavigation(array &$page_top): void { ->addCacheableDependency($this->configFactory->get('navigation.block_layout')); $cacheability->applyTo($build); - $build[0] += [ + $module_path = $this->requestStack->getCurrentRequest()->getBasePath() . '/' . $this->moduleExtensionList->getPath('navigation'); + $asset_url = $module_path . '/assets/fonts/inter-var.woff2'; + + $defaults = [ '#hide_logo' => $logo_provider === self::LOGO_PROVIDER_HIDE, '#access' => $this->currentUser->hasPermission('access navigation'), + '#attached' => [ + 'html_head_link' => [ + [ + [ + 'rel' => 'preload', + 'href' => $asset_url, + 'as' => 'font', + 'crossorigin' => 'anonymous', + ], + ], + ], + ], ]; + $build[0] = NestedArray::mergeDeepArray([$build[0], $defaults]); $page_top['navigation'] = $build; if ($logo_provider === self::LOGO_PROVIDER_CUSTOM) { @@ -148,7 +169,7 @@ public function buildNavigation(array &$page_top): void { * @param array $page_top * A renderable array representing the top of the page. * - * @see toolbar_page_top() + * @see navigation_page_top() * @see hook_page_top() */ public function buildTopBar(array &$page_top): void {