function NavigationRenderer::buildNavigation

Same name in other branches
  1. 11.x 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 101

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 = [
        '#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_managed_fid = $logo_settings->get('logo_managed');
        if (isset($logo_managed_fid[0]) && $logo_managed_fid[0] > 0) {
            $logo_managed = File::load($logo_managed_fid[0]);
            if ($logo_managed instanceof File) {
                $logo_managed_uri = $logo_managed->getFileUri();
                $logo_managed_url = $this->fileUrlGenerator
                    ->generateAbsoluteString($logo_managed_uri);
                $page_top['navigation']['#logo_path'] = $logo_managed_url;
                $image = $this->imageFactory
                    ->get($logo_managed_uri);
                if ($image->isValid()) {
                    $page_top['navigation']['#logo_width'] = $image->getWidth();
                    $page_top['navigation']['#logo_height'] = $image->getHeight();
                }
            }
        }
    }
}

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