2015-08-17 17:00:26 -07:00
< ? php
/**
* @ file
* Hooks provided by the toolbar module .
*/
2016-04-20 09:56:34 -07:00
2015-08-17 17:00:26 -07:00
use Drupal\Core\Url ;
/**
* @ addtogroup hooks
* @ {
*/
/**
* Add items to the toolbar menu .
*
* The toolbar is a container for administrative and site - global interactive
* components .
*
* The toolbar provides a common styling for items denoted by the
* . toolbar - tab class .
*
* The toolbar provides a construct called a 'tray' . The tray is a container
* for content . The tray may be associated with a toggle in the administration
* bar . The toggle shows or hides the tray and is optimized for small and
* large screens . To create this association , hook_toolbar () returns one or
* more render elements of type 'toolbar_item' , containing the toggle and tray
* elements in its 'tab' and 'tray' properties .
*
* The following properties are available :
* - 'tab' : A renderable array .
* - 'tray' : Optional . A renderable array .
* - '#weight' : Optional . Integer weight used for sorting toolbar items in
* administration bar area .
*
* This hook is invoked in toolbar_pre_render () .
*
* @ return
* An array of toolbar items , keyed by unique identifiers such as 'home' or
* 'administration' , or the short name of the module implementing the hook .
* The corresponding value is a render element of type 'toolbar_item' .
*
* @ see toolbar_pre_render ()
* @ ingroup toolbar_tabs
*/
function hook_toolbar () {
2017-04-13 15:53:35 +01:00
$items = [];
2015-08-17 17:00:26 -07:00
// Add a search field to the toolbar. The search field employs no toolbar
// module theming functions.
2017-04-13 15:53:35 +01:00
$items [ 'global_search' ] = [
2015-08-17 17:00:26 -07:00
'#type' => 'toolbar_item' ,
2017-04-13 15:53:35 +01:00
'tab' => [
2015-08-17 17:00:26 -07:00
'#type' => 'search' ,
2017-04-13 15:53:35 +01:00
'#attributes' => [
2015-08-17 17:00:26 -07:00
'placeholder' => t ( 'Search the site' ),
2017-04-13 15:53:35 +01:00
'class' => [ 'search-global' ],
],
],
2015-08-17 17:00:26 -07:00
'#weight' => 200 ,
// Custom CSS, JS or a library can be associated with the toolbar item.
2017-04-13 15:53:35 +01:00
'#attached' => [
'library' => [
2015-08-17 17:00:26 -07:00
'search/global' ,
2017-04-13 15:53:35 +01:00
],
],
];
2015-08-17 17:00:26 -07:00
// The 'Home' tab is a simple link, which is wrapped in markup associated
// with a visual tab styling.
2017-04-13 15:53:35 +01:00
$items [ 'home' ] = [
2015-08-17 17:00:26 -07:00
'#type' => 'toolbar_item' ,
2017-04-13 15:53:35 +01:00
'tab' => [
2015-08-17 17:00:26 -07:00
'#type' => 'link' ,
'#title' => t ( 'Home' ),
'#url' => Url :: fromRoute ( '<front>' ),
2017-04-13 15:53:35 +01:00
'#options' => [
'attributes' => [
2015-08-17 17:00:26 -07:00
'title' => t ( 'Home page' ),
2017-04-13 15:53:35 +01:00
'class' => [ 'toolbar-icon' , 'toolbar-icon-home' ],
],
],
],
2015-08-17 17:00:26 -07:00
'#weight' => - 20 ,
2017-04-13 15:53:35 +01:00
];
2015-08-17 17:00:26 -07:00
// A tray may be associated with a tab.
//
// When the tab is activated, the tray will become visible, either in a
// horizontal or vertical orientation on the screen.
//
// The tray should contain a renderable array. An optional #heading property
// can be passed. This text is written to a heading tag in the tray as a
// landmark for accessibility.
2017-04-13 15:53:35 +01:00
$items [ 'commerce' ] = [
2015-08-17 17:00:26 -07:00
'#type' => 'toolbar_item' ,
2017-04-13 15:53:35 +01:00
'tab' => [
2015-08-17 17:00:26 -07:00
'#type' => 'link' ,
'#title' => t ( 'Shopping cart' ),
'#url' => Url :: fromRoute ( 'cart' ),
2017-04-13 15:53:35 +01:00
'#options' => [
'attributes' => [
2015-08-17 17:00:26 -07:00
'title' => t ( 'Shopping cart' ),
2017-04-13 15:53:35 +01:00
],
],
],
'tray' => [
2015-08-17 17:00:26 -07:00
'#heading' => t ( 'Shopping cart actions' ),
2017-04-13 15:53:35 +01:00
'shopping_cart' => [
2015-08-17 17:00:26 -07:00
'#theme' => 'item_list' ,
2018-11-23 12:29:20 +00:00
'#items' => [ /* An item list renderable array */ ],
2017-04-13 15:53:35 +01:00
],
],
2015-08-17 17:00:26 -07:00
'#weight' => 150 ,
2017-04-13 15:53:35 +01:00
];
2015-08-17 17:00:26 -07:00
// The tray can be used to render arbitrary content.
//
// A renderable array passed to the 'tray' property will be rendered outside
// the administration bar but within the containing toolbar element.
//
// If the default behavior and styling of a toolbar tray is not desired, one
// can render content to the toolbar element and apply custom theming and
// behaviors.
2017-04-13 15:53:35 +01:00
$items [ 'user_messages' ] = [
2015-08-17 17:00:26 -07:00
// Include the toolbar_tab_wrapper to style the link like a toolbar tab.
// Exclude the theme wrapper if custom styling is desired.
'#type' => 'toolbar_item' ,
2017-04-13 15:53:35 +01:00
'tab' => [
2015-08-17 17:00:26 -07:00
'#type' => 'link' ,
'#theme' => 'user_message_toolbar_tab' ,
2017-04-13 15:53:35 +01:00
'#theme_wrappers' => [],
2015-08-17 17:00:26 -07:00
'#title' => t ( 'Messages' ),
'#url' => Url :: fromRoute ( 'user.message' ),
2017-04-13 15:53:35 +01:00
'#options' => [
'attributes' => [
2015-08-17 17:00:26 -07:00
'title' => t ( 'Messages' ),
2017-04-13 15:53:35 +01:00
],
],
],
'tray' => [
2015-08-17 17:00:26 -07:00
'#heading' => t ( 'User messages' ),
2017-04-13 15:53:35 +01:00
'messages' => [ /* renderable content */ ],
],
2015-08-17 17:00:26 -07:00
'#weight' => 125 ,
2017-04-13 15:53:35 +01:00
];
2015-08-17 17:00:26 -07:00
return $items ;
}
/**
* Alter the toolbar menu after hook_toolbar () is invoked .
*
2018-11-23 12:29:20 +00:00
* This hook is invoked by Toolbar :: preRenderToolbar () immediately after
* hook_toolbar () . The toolbar definitions are passed in by reference . Each
* element of the $items array is one item returned by a module from
* hook_toolbar () . Additional items may be added , or existing items altered .
2015-08-17 17:00:26 -07:00
*
* @ param $items
* Associative array of toolbar menu definitions returned from hook_toolbar () .
*/
function hook_toolbar_alter ( & $items ) {
// Move the User tab to the right.
$items [ 'commerce' ][ '#weight' ] = 5 ;
}
/**
* @ } End of " addtogroup hooks " .
*/