5 * Administration toolbar for quick access to top level administration items.
8 use Drupal\Core\Cache\CacheableMetadata;
9 use Drupal\Core\Menu\MenuTreeParameters;
10 use Drupal\Core\Render\Element;
11 use Drupal\Core\Routing\RouteMatchInterface;
12 use Drupal\Core\Template\Attribute;
13 use Drupal\Component\Utility\Crypt;
17 * Implements hook_help().
19 function toolbar_help($route_name, RouteMatchInterface $route_match) {
20 switch ($route_name) {
21 case 'help.page.toolbar':
22 $output = '<h3>' . t('About') . '</h3>';
23 $output .= '<p>' . t('The Toolbar module provides a toolbar for site administrators, which displays tabs and trays provided by the Toolbar module itself and other modules. For more information, see the <a href=":toolbar_docs">online documentation for the Toolbar module</a>.', [':toolbar_docs' => 'https://www.drupal.org/documentation/modules/toolbar']) . '</p>';
24 $output .= '<h4>' . t('Terminology') . '</h4>';
26 $output .= '<dt>' . t('Tabs') . '</dt>';
27 $output .= '<dd>' . t('Tabs are buttons, displayed in a bar across the top of the screen. Some tabs execute an action (such as starting Edit mode), while other tabs toggle which tray is open.') . '</dd>';
28 $output .= '<dt>' . t('Trays') . '</dt>';
29 $output .= '<dd>' . t('Trays are usually lists of links, which can be hierarchical like a menu. If a tray has been toggled open, it is displayed either vertically or horizontally below the tab bar, depending on the browser width. Only one tray may be open at a time. If you click another tab, that tray will replace the tray being displayed. In wide browser widths, the user has the ability to toggle from vertical to horizontal, using a link at the bottom or right of the tray. Hierarchical menus only have open/close behavior in vertical mode; if you display a tray containing a hierarchical menu horizontally, only the top-level links will be available.') . '</dd>';
36 * Implements hook_theme().
38 function toolbar_theme($existing, $type, $theme, $path) {
40 'render element' => 'element',
42 $items['menu__toolbar'] = [
43 'base hook' => 'menu',
44 'variables' => ['items' => [], 'attributes' => []],
51 * Implements hook_page_top().
53 * Add admin toolbar to the top of the page automatically.
55 function toolbar_page_top(array &$page_top) {
56 $page_top['toolbar'] = [
58 '#access' => \Drupal::currentUser()->hasPermission('access toolbar'),
60 'keys' => ['toolbar'],
61 'contexts' => ['user.permissions'],
67 * Prepares variables for administration toolbar templates.
69 * Default template: toolbar.html.twig.
71 * @param array $variables
72 * An associative array containing:
73 * - element: An associative array containing the properties and children of
74 * the tray. Properties used: #children, #attributes and #bar.
76 function template_preprocess_toolbar(&$variables) {
77 $element = $variables['element'];
79 // Prepare the toolbar attributes.
80 $variables['attributes'] = $element['#attributes'];
81 $variables['toolbar_attributes'] = new Attribute($element['#bar']['#attributes']);
82 $variables['toolbar_heading'] = $element['#bar']['#heading'];
84 // Prepare the trays and tabs for each toolbar item as well as the remainder
85 // variable that will hold any non-tray, non-tab elements.
86 $variables['trays'] = [];
87 $variables['tabs'] = [];
88 $variables['remainder'] = [];
89 foreach (Element::children($element) as $key) {
90 // Early rendering to collect the wrapper attributes from
91 // ToolbarItem elements.
92 if (!empty($element[$key])) {
93 Drupal::service('renderer')->render($element[$key]);
96 if (isset($element[$key]['tray'])) {
98 if (!empty($element[$key]['tray']['#wrapper_attributes'])) {
99 $attributes = $element[$key]['tray']['#wrapper_attributes'];
101 $variables['trays'][$key] = [
102 'links' => $element[$key]['tray'],
103 'attributes' => new Attribute($attributes),
105 if (array_key_exists('#heading', $element[$key]['tray'])) {
106 $variables['trays'][$key]['label'] = $element[$key]['tray']['#heading'];
111 if (isset($element[$key]['tab'])) {
113 // Pass the wrapper attributes along.
114 if (!empty($element[$key]['#wrapper_attributes'])) {
115 $attributes = $element[$key]['#wrapper_attributes'];
118 $variables['tabs'][$key] = [
119 'link' => $element[$key]['tab'],
120 'attributes' => new Attribute($attributes),
124 // Add other non-tray, non-tab child elements to the remainder variable for
126 foreach (Element::children($element[$key]) as $child_key) {
127 if (!in_array($child_key, ['tray', 'tab'])) {
128 $variables['remainder'][$key][$child_key] = $element[$key][$child_key];
135 * Implements hook_toolbar().
137 function toolbar_toolbar() {
138 // The 'Home' tab is a simple link, with no corresponding tray.
140 '#type' => 'toolbar_item',
143 '#title' => t('Back to site'),
144 '#url' => Url::fromRoute('<front>'),
146 'title' => t('Return to site content'),
147 'class' => ['toolbar-icon', 'toolbar-icon-escape-admin'],
148 'data-toolbar-escape-admin' => TRUE,
151 '#wrapper_attributes' => [
152 'class' => ['hidden', 'home-toolbar-tab'],
156 'toolbar/toolbar.escapeAdmin',
162 // To conserve bandwidth, we only include the top-level links in the HTML.
163 // The subtrees are fetched through a JSONP script that is generated at the
164 // toolbar_subtrees route. We provide the JavaScript requesting that JSONP
165 // script here with the hash parameter that is needed for that route.
166 // @see toolbar_subtrees_jsonp()
167 list($hash, $hash_cacheability) = _toolbar_get_subtrees_hash();
168 $subtrees_attached['drupalSettings']['toolbar'] = [
169 'subtreesHash' => $hash,
172 // The administration element has a link that is themed to correspond to
173 // a toolbar tray. The tray contains the full administrative menu of the site.
174 $items['administration'] = [
175 '#type' => 'toolbar_item',
178 '#title' => t('Manage'),
179 '#url' => Url::fromRoute('system.admin'),
181 'title' => t('Admin menu'),
182 'class' => ['toolbar-icon', 'toolbar-icon-menu'],
183 // A data attribute that indicates to the client to defer loading of
184 // the admin menu subtrees until this tab is activated. Admin menu
185 // subtrees will not render to the DOM if this attribute is removed.
186 // The value of the attribute is intentionally left blank. Only the
187 // presence of the attribute is necessary.
188 'data-drupal-subtrees' => '',
192 '#heading' => t('Administration menu'),
193 '#attached' => $subtrees_attached,
194 'toolbar_administration' => [
196 'toolbar_prerender_toolbar_administration_tray',
198 '#type' => 'container',
200 'class' => ['toolbar-menu-administration'],
206 $hash_cacheability->applyTo($items['administration']);
212 * Renders the toolbar's administration tray.
214 * @param array $element
215 * A renderable array.
218 * The updated renderable array.
220 * @see drupal_render()
222 function toolbar_prerender_toolbar_administration_tray(array $element) {
223 $menu_tree = \Drupal::service('toolbar.menu_tree');
224 // Load the administrative menu. The first level is the "Administration" link.
225 // In order to load the children of that link, start and end on the second
227 $parameters = new MenuTreeParameters();
228 $parameters->setMinDepth(2)->setMaxDepth(2)->onlyEnabledLinks();
229 // @todo Make the menu configurable in https://www.drupal.org/node/1869638.
230 $tree = $menu_tree->load('admin', $parameters);
232 ['callable' => 'menu.default_tree_manipulators:checkAccess'],
233 ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
234 ['callable' => 'toolbar_menu_navigation_links'],
236 $tree = $menu_tree->transform($tree, $manipulators);
237 $element['administration_menu'] = $menu_tree->build($tree);
242 * Adds toolbar-specific attributes to the menu link tree.
244 * @param \Drupal\Core\Menu\MenuLinkTreeElement[] $tree
245 * The menu link tree to manipulate.
247 * @return \Drupal\Core\Menu\MenuLinkTreeElement[]
248 * The manipulated menu link tree.
250 function toolbar_menu_navigation_links(array $tree) {
251 foreach ($tree as $element) {
252 if ($element->subtree) {
253 toolbar_menu_navigation_links($element->subtree);
256 // Make sure we have a path specific ID in place, so we can attach icons
257 // and behaviors to the menu links.
258 $link = $element->link;
259 $url = $link->getUrlObject();
260 if (!$url->isRouted()) {
261 // This is an unusual case, so just get a distinct, safe string.
262 $id = substr(Crypt::hashBase64($url->getUri()), 0, 16);
265 $id = str_replace(['.', '<', '>'], ['-', '', ''], $url->getRouteName());
268 // Get the non-localized title to make the icon class.
269 $definition = $link->getPluginDefinition();
271 $element->options['attributes']['id'] = 'toolbar-link-' . $id;
272 $element->options['attributes']['class'][] = 'toolbar-icon';
273 $element->options['attributes']['class'][] = 'toolbar-icon-' . strtolower(str_replace(['.', ' ', '_'], ['-', '-', '-'], $definition['id']));
274 $element->options['attributes']['title'] = $link->getDescription();
280 * Returns the rendered subtree of each top-level toolbar link.
283 * An array with the following key-value pairs:
284 * - 'subtrees': the rendered subtrees
285 * - 'cacheability: the associated cacheability.
287 function toolbar_get_rendered_subtrees() {
289 '#pre_render' => ['_toolbar_do_get_rendered_subtrees'],
292 'toolbar_rendered_subtrees',
295 '#cache_properties' => ['#subtrees'],
297 \Drupal::service('renderer')->renderPlain($data);
298 return [$data['#subtrees'], CacheableMetadata::createFromRenderArray($data)];
302 * #pre_render callback for toolbar_get_rendered_subtrees().
304 function _toolbar_do_get_rendered_subtrees(array $data) {
305 $menu_tree = \Drupal::service('toolbar.menu_tree');
306 // Load the administration menu. The first level is the "Administration" link.
307 // In order to load the children of that link and the subsequent two levels,
308 // start at the second level and end at the fourth.
309 $parameters = new MenuTreeParameters();
310 $parameters->setMinDepth(2)->setMaxDepth(4)->onlyEnabledLinks();
311 // @todo Make the menu configurable in https://www.drupal.org/node/1869638.
312 $tree = $menu_tree->load('admin', $parameters);
314 ['callable' => 'menu.default_tree_manipulators:checkAccess'],
315 ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
316 ['callable' => 'toolbar_menu_navigation_links'],
318 $tree = $menu_tree->transform($tree, $manipulators);
320 // Calculated the combined cacheability of all subtrees.
321 $cacheability = new CacheableMetadata();
322 foreach ($tree as $element) {
323 /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
324 $link = $element->link;
325 if ($element->subtree) {
326 $subtree = $menu_tree->build($element->subtree);
327 $output = \Drupal::service('renderer')->renderPlain($subtree);
328 $cacheability = $cacheability->merge(CacheableMetadata::createFromRenderArray($subtree));
333 // Many routes have dots as route name, while some special ones like <front>
334 // have <> characters in them.
335 $url = $link->getUrlObject();
336 $id = str_replace(['.', '<', '>'], ['-', '', '' ], $url->isRouted() ? $url->getRouteName() : $url->getUri());
338 $subtrees[$id] = $output;
341 // Store the subtrees, along with the cacheability metadata.
342 $cacheability->applyTo($data);
343 $data['#subtrees'] = $subtrees;
349 * Returns the hash of the per-user rendered toolbar subtrees.
352 * The hash of the admin_menu subtrees.
354 function _toolbar_get_subtrees_hash() {
355 list($subtrees, $cacheability) = toolbar_get_rendered_subtrees();
356 $hash = Crypt::hashBase64(serialize($subtrees));
357 return [$hash, $cacheability];