5 * Hooks provided by the toolbar module.
16 * Add items to the toolbar menu.
18 * The toolbar is a container for administrative and site-global interactive
21 * The toolbar provides a common styling for items denoted by the
24 * The toolbar provides a construct called a 'tray'. The tray is a container
25 * for content. The tray may be associated with a toggle in the administration
26 * bar. The toggle shows or hides the tray and is optimized for small and
27 * large screens. To create this association, hook_toolbar() returns one or
28 * more render elements of type 'toolbar_item', containing the toggle and tray
29 * elements in its 'tab' and 'tray' properties.
31 * The following properties are available:
32 * - 'tab': A renderable array.
33 * - 'tray': Optional. A renderable array.
34 * - '#weight': Optional. Integer weight used for sorting toolbar items in
35 * administration bar area.
37 * This hook is invoked in toolbar_pre_render().
40 * An array of toolbar items, keyed by unique identifiers such as 'home' or
41 * 'administration', or the short name of the module implementing the hook.
42 * The corresponding value is a render element of type 'toolbar_item'.
44 * @see toolbar_pre_render()
45 * @ingroup toolbar_tabs
47 function hook_toolbar() {
50 // Add a search field to the toolbar. The search field employs no toolbar
51 // module theming functions.
52 $items['global_search'] = [
53 '#type' => 'toolbar_item',
57 'placeholder' => t('Search the site'),
58 'class' => ['search-global'],
62 // Custom CSS, JS or a library can be associated with the toolbar item.
70 // The 'Home' tab is a simple link, which is wrapped in markup associated
71 // with a visual tab styling.
73 '#type' => 'toolbar_item',
76 '#title' => t('Home'),
77 '#url' => Url::fromRoute('<front>'),
80 'title' => t('Home page'),
81 'class' => ['toolbar-icon', 'toolbar-icon-home'],
88 // A tray may be associated with a tab.
90 // When the tab is activated, the tray will become visible, either in a
91 // horizontal or vertical orientation on the screen.
93 // The tray should contain a renderable array. An optional #heading property
94 // can be passed. This text is written to a heading tag in the tray as a
95 // landmark for accessibility.
96 $items['commerce'] = [
97 '#type' => 'toolbar_item',
100 '#title' => t('Shopping cart'),
101 '#url' => Url::fromRoute('cart'),
104 'title' => t('Shopping cart'),
109 '#heading' => t('Shopping cart actions'),
111 '#theme' => 'item_list',
112 '#items' => [/* An item list renderable array */],
118 // The tray can be used to render arbitrary content.
120 // A renderable array passed to the 'tray' property will be rendered outside
121 // the administration bar but within the containing toolbar element.
123 // If the default behavior and styling of a toolbar tray is not desired, one
124 // can render content to the toolbar element and apply custom theming and
126 $items['user_messages'] = [
127 // Include the toolbar_tab_wrapper to style the link like a toolbar tab.
128 // Exclude the theme wrapper if custom styling is desired.
129 '#type' => 'toolbar_item',
132 '#theme' => 'user_message_toolbar_tab',
133 '#theme_wrappers' => [],
134 '#title' => t('Messages'),
135 '#url' => Url::fromRoute('user.message'),
138 'title' => t('Messages'),
143 '#heading' => t('User messages'),
144 'messages' => [/* renderable content */],
153 * Alter the toolbar menu after hook_toolbar() is invoked.
155 * This hook is invoked by Toolbar::preRenderToolbar() immediately after
156 * hook_toolbar(). The toolbar definitions are passed in by reference. Each
157 * element of the $items array is one item returned by a module from
158 * hook_toolbar(). Additional items may be added, or existing items altered.
161 * Associative array of toolbar menu definitions returned from hook_toolbar().
163 function hook_toolbar_alter(&$items) {
164 // Move the User tab to the right.
165 $items['commerce']['#weight'] = 5;
169 * @} End of "addtogroup hooks".