function NavigationRenderer::buildNavigation

Same name in other branches
  1. 10 core/modules/navigation/src/NavigationRenderer.php \Drupal\navigation\NavigationRenderer::buildNavigation()

Build out the navigation bar.

Parameters

array $page_top: A renderable array representing the top of the page.

See also

toolbar_page_top()

hook_page_top()

File

core/modules/navigation/src/NavigationRenderer.php, line 100

Class

NavigationRenderer
Handle rendering for different pieces of the navigation.

Namespace

Drupal\navigation

Code

public function buildNavigation(array &$page_top) : void {
    $logo_settings = $this->configFactory
        ->get('navigation.settings');
    $logo_provider = $logo_settings->get('logo.provider');
    $cacheability = new CacheableMetadata();
    $contexts = [
        'navigation' => new Context(ContextDefinition::create('string'), 'navigation'),
    ];
    $storage = $this->sectionStorageManager
        ->findByContext($contexts, $cacheability);
    $build = [];
    if ($storage) {
        foreach ($storage->getSections() as $delta => $section) {
            $build[$delta] = $section->toRenderArray([]);
        }
    }
    // The render array is built based on decisions made by SectionStorage
    // plugins and therefore it needs to depend on the accumulated
    // cacheability of those decisions.
    $cacheability->addCacheableDependency($logo_settings)
        ->addCacheableDependency($this->configFactory
        ->get('navigation.block_layout'));
    $cacheability->applyTo($build);
    $module_path = $this->requestStack
        ->getCurrentRequest()
        ->getBasePath() . '/' . $this->moduleExtensionList
        ->getPath('navigation');
    $asset_url = $module_path . '/assets/fonts/inter-var.woff2';
    $defaults = [
        'settings' => [
            'hide_logo' => $logo_provider === self::LOGO_PROVIDER_HIDE,
        ],
        '#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) {
        $logo_path = $logo_settings->get('logo.path');
        if (!empty($logo_path) && is_file($logo_path)) {
            $logo_managed_url = $this->fileUrlGenerator
                ->generateAbsoluteString($logo_path);
            $image = $this->imageFactory
                ->get($logo_path);
            $page_top['navigation'][0]['settings']['logo_path'] = $logo_managed_url;
            if ($image->isValid()) {
                $page_top['navigation'][0]['settings']['logo_width'] = $image->getWidth();
                $page_top['navigation'][0]['settings']['logo_height'] = $image->getHeight();
            }
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.