function NavigationRenderer::doBuildNavigation

Pre-render callback for ::buildNavigation.

Attributes

#[TrustedCallback]

File

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

Class

NavigationRenderer
Handle rendering for different pieces of the navigation.

Namespace

Drupal\navigation

Code

public function doBuildNavigation() : array {
  $build = [];
  $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);
  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,
  ]);
  $build[0]['content_top'] = $this->getContentTop();
  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);
      $build[0]['#settings']['logo_path'] = $logo_managed_url;
      if ($image->isValid()) {
        $build[0]['#settings']['logo_width'] = $image->getWidth();
        $build[0]['#settings']['logo_height'] = $image->getHeight();
      }
    }
  }
  return $build;
}

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