3 namespace Drupal\toolbar\Element;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Render\Element\RenderElement;
7 use Drupal\Core\Render\Element;
10 * Provides a render element for the default Drupal toolbar.
12 * @RenderElement("toolbar")
14 class Toolbar extends RenderElement {
19 public function getInfo() {
20 $class = get_class($this);
23 [$class, 'preRenderToolbar'],
25 '#theme' => 'toolbar',
31 // Metadata for the toolbar wrapping element.
33 // The id cannot be simply "toolbar" or it will clash with the
34 // simpletest tests listing which produces a checkbox with attribute
36 'id' => 'toolbar-administration',
38 'aria-label' => $this->t('Site administration toolbar'),
40 // Metadata for the administration bar.
42 '#heading' => $this->t('Toolbar items'),
44 'id' => 'toolbar-bar',
45 'role' => 'navigation',
46 'aria-label' => $this->t('Toolbar items'),
53 * Builds the Toolbar as a structured array ready for drupal_render().
55 * Since building the toolbar takes some time, it is done just prior to
56 * rendering to ensure that it is built only if it will be displayed.
58 * @param array $element
64 * @see toolbar_page_top()
66 public static function preRenderToolbar($element) {
67 // Get the configured breakpoints to switch from vertical to horizontal
68 // toolbar presentation.
69 $breakpoints = static::breakpointManager()->getBreakpointsByGroup('toolbar');
70 if (!empty($breakpoints)) {
72 foreach ($breakpoints as $id => $breakpoint) {
73 $media_queries[$id] = $breakpoint->getMediaQuery();
76 $element['#attached']['drupalSettings']['toolbar']['breakpoints'] = $media_queries;
79 $module_handler = static::moduleHandler();
80 // Get toolbar items from all modules that implement hook_toolbar().
81 $items = $module_handler->invokeAll('toolbar');
82 // Allow for altering of hook_toolbar().
83 $module_handler->alter('toolbar', $items);
85 uasort($items, ['\Drupal\Component\Utility\SortArray', 'sortByWeightProperty']);
87 // Merge in the original toolbar values.
88 $element = array_merge($element, $items);
90 // Assign each item a unique ID, based on its key.
91 foreach (Element::children($element) as $key) {
92 $element[$key]['#id'] = Html::getId('toolbar-item-' . $key);
99 * Wraps the breakpoint manager.
101 * @return \Drupal\breakpoint\BreakpointManagerInterface
103 protected static function breakpointManager() {
104 return \Drupal::service('breakpoint.manager');
108 * Wraps the module handler.
110 * @return \Drupal\Core\Extension\ModuleHandlerInterface
112 protected static function moduleHandler() {
113 return \Drupal::moduleHandler();