summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/navigation/src/Element/TopBar.php
blob: 398fd083608e6dead92634e186d7ea29fb434314 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php

declare(strict_types=1);

namespace Drupal\navigation\Element;

use Drupal\Core\Render\Attribute\RenderElement;
use Drupal\Core\Render\Element\RenderElementBase;
use Drupal\navigation\TopBarItemManagerInterface;
use Drupal\navigation\TopBarRegion;

/**
 * Provides a render element for the default Drupal toolbar.
 */
#[RenderElement('top_bar')]
class TopBar extends RenderElementBase {

  /**
   * {@inheritdoc}
   */
  public function getInfo(): array {
    return [
      '#pre_render' => [
        [static::class, 'preRenderTopBar'],
      ],
      '#theme' => 'top_bar',
      '#attached' => [
        'library' => [
          'navigation/internal.navigation',
        ],
      ],
    ];
  }

  /**
   * Builds the TopBar as a structured array ready for rendering.
   *
   * Since building the TopBar takes some time, it is done just prior to
   * rendering to ensure that it is built only if it will be displayed.
   *
   * @param array $element
   *   A renderable array.
   *
   * @return array
   *   A renderable array.
   *
   * @see navigation_page_top()
   */
  public static function preRenderTopBar($element): array {
    $top_bar_item_manager = static::topBarItemManager();

    // Group the items by region.
    foreach (TopBarRegion::cases() as $region) {
      $items = $top_bar_item_manager->getRenderedTopBarItemsByRegion($region);
      $element = array_merge($element, [$region->value => $items]);
    }

    return $element;
  }

  /**
   * Wraps the top bar item manager.
   *
   * @return \Drupal\navigation\TopBarItemManager
   *   The top bar item manager.
   */
  protected static function topBarItemManager(): TopBarItemManagerInterface {
    return \Drupal::service(TopBarItemManagerInterface::class);
  }

}