class TopBar

Provides a render element for the default Drupal toolbar.

Attributes

#[RenderElement('top_bar')]

Hierarchy

Expanded class hierarchy of TopBar

File

core/modules/navigation/src/Element/TopBar.php, line 15

Namespace

Drupal\navigation\Element
View source
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);
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 7
PluginInspectionInterface::getPluginId public function Gets the plugin ID of the plugin instance. 3
RenderElementBase::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript. 2
RenderElementBase::preRenderGroup public static function Adds members of this group as actual elements for rendering. 2
RenderElementBase::processAjaxForm public static function Form element processing handler for the #ajax form property. 3
RenderElementBase::processGroup public static function Arranges elements into groups. 2
RenderElementBase::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes 2
TopBar::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
TopBar::preRenderTopBar public static function Builds the TopBar as a structured array ready for rendering.
TopBar::topBarItemManager protected static function Wraps the top bar item manager.

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