Version 1
[yaffs-website] / web / core / modules / toolbar / src / Element / Toolbar.php
1 <?php
2
3 namespace Drupal\toolbar\Element;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Render\Element\RenderElement;
7 use Drupal\Core\Render\Element;
8
9 /**
10  * Provides a render element for the default Drupal toolbar.
11  *
12  * @RenderElement("toolbar")
13  */
14 class Toolbar extends RenderElement {
15
16   /**
17    * {@inheritdoc}
18    */
19   public function getInfo() {
20     $class = get_class($this);
21     return [
22       '#pre_render' => [
23         [$class, 'preRenderToolbar'],
24       ],
25       '#theme' => 'toolbar',
26       '#attached' => [
27         'library' => [
28           'toolbar/toolbar',
29         ],
30       ],
31       // Metadata for the toolbar wrapping element.
32       '#attributes' => [
33         // The id cannot be simply "toolbar" or it will clash with the
34         // simpletest tests listing which produces a checkbox with attribute
35         // id="toolbar".
36         'id' => 'toolbar-administration',
37         'role' => 'group',
38         'aria-label' => $this->t('Site administration toolbar'),
39       ],
40       // Metadata for the administration bar.
41       '#bar' => [
42         '#heading' => $this->t('Toolbar items'),
43         '#attributes' => [
44           'id' => 'toolbar-bar',
45           'role' => 'navigation',
46           'aria-label' => $this->t('Toolbar items'),
47         ],
48       ],
49     ];
50   }
51
52   /**
53    * Builds the Toolbar as a structured array ready for drupal_render().
54    *
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.
57    *
58    * @param array $element
59    *   A renderable array.
60    *
61    * @return array
62    *   A renderable array.
63    *
64    * @see toolbar_page_top()
65    */
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)) {
71       $media_queries = [];
72       foreach ($breakpoints as $id => $breakpoint) {
73         $media_queries[$id] = $breakpoint->getMediaQuery();
74       }
75
76       $element['#attached']['drupalSettings']['toolbar']['breakpoints'] = $media_queries;
77     }
78
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);
84     // Sort the children.
85     uasort($items, ['\Drupal\Component\Utility\SortArray', 'sortByWeightProperty']);
86
87     // Merge in the original toolbar values.
88     $element = array_merge($element, $items);
89
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);
93     }
94
95     return $element;
96   }
97
98   /**
99    * Wraps the breakpoint manager.
100    *
101    * @return \Drupal\breakpoint\BreakpointManagerInterface
102    */
103   protected static function breakpointManager() {
104     return \Drupal::service('breakpoint.manager');
105   }
106
107   /**
108    * Wraps the module handler.
109    *
110    * @return \Drupal\Core\Extension\ModuleHandlerInterface
111    */
112   protected static function moduleHandler() {
113     return \Drupal::moduleHandler();
114   }
115
116 }