toolbar.api.php 4.86 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Hooks provided by the toolbar module.
6
 */
7

8
use Drupal\Core\Url;
9 10 11 12 13 14 15

/**
 * @addtogroup hooks
 * @{
 */

/**
16
 * Add items to the toolbar menu.
17
 *
18
 * The toolbar is a container for administrative and site-global interactive
19
 * components.
20
 *
21
 * The toolbar provides a common styling for items denoted by the
22
 * .toolbar-tab class.
23 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 27 28 29 30 31 32 33 34
 * 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
35
 *     administration bar area.
36 37
 *
 * This hook is invoked in toolbar_pre_render().
38 39 40 41
 *
 * @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.
42
 *   The corresponding value is a render element of type 'toolbar_item'.
43
 *
44
 * @see toolbar_pre_render()
45 46 47
 * @ingroup toolbar_tabs
 */
function hook_toolbar() {
48
  $items = [];
49

50 51
  // Add a search field to the toolbar. The search field employs no toolbar
  // module theming functions.
52
  $items['global_search'] = [
53
    '#type' => 'toolbar_item',
54
    'tab' => [
55
      '#type' => 'search',
56
      '#attributes' => [
57
        'placeholder' => t('Search the site'),
58 59 60
        'class' => ['search-global'],
      ],
    ],
61 62
    '#weight' => 200,
    // Custom CSS, JS or a library can be associated with the toolbar item.
63 64
    '#attached' => [
      'library' => [
65
        'search/global',
66 67 68
      ],
    ],
  ];
69 70 71

  // The 'Home' tab is a simple link, which is wrapped in markup associated
  // with a visual tab styling.
72
  $items['home'] = [
73
    '#type' => 'toolbar_item',
74
    'tab' => [
75 76
      '#type' => 'link',
      '#title' => t('Home'),
77
      '#url' => Url::fromRoute('<front>'),
78 79
      '#options' => [
        'attributes' => [
80
          'title' => t('Home page'),
81 82 83 84
          'class' => ['toolbar-icon', 'toolbar-icon-home'],
        ],
      ],
    ],
85
    '#weight' => -20,
86
  ];
87

88 89 90 91 92 93 94 95
  // 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.
96
  $items['commerce'] = [
97
    '#type' => 'toolbar_item',
98
    'tab' => [
99 100
      '#type' => 'link',
      '#title' => t('Shopping cart'),
101
      '#url' => Url::fromRoute('cart'),
102 103
      '#options' => [
        'attributes' => [
104
          'title' => t('Shopping cart'),
105 106 107 108
        ],
      ],
    ],
    'tray' => [
109
      '#heading' => t('Shopping cart actions'),
110
      'shopping_cart' => [
111
        '#theme' => 'item_list',
112
        '#items' => [/* An item list renderable array */],
113 114
      ],
    ],
115
    '#weight' => 150,
116
  ];
117

118
  // The tray can be used to render arbitrary content.
119 120 121 122 123 124 125
  //
  // 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.
126
  $items['user_messages'] = [
127 128 129
    // 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',
130
    'tab' => [
131 132
      '#type' => 'link',
      '#theme' => 'user_message_toolbar_tab',
133
      '#theme_wrappers' => [],
134
      '#title' => t('Messages'),
135
      '#url' => Url::fromRoute('user.message'),
136 137
      '#options' => [
        'attributes' => [
138
          'title' => t('Messages'),
139 140 141 142
        ],
      ],
    ],
    'tray' => [
143
      '#heading' => t('User messages'),
144 145
      'messages' => [/* renderable content */],
    ],
146
    '#weight' => 125,
147
  ];
148 149 150 151 152

  return $items;
}

/**
153
 * Alter the toolbar menu after hook_toolbar() is invoked.
154
 *
155 156 157 158
 * 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.
159 160
 *
 * @param $items
161
 *   Associative array of toolbar menu definitions returned from hook_toolbar().
162 163 164
 */
function hook_toolbar_alter(&$items) {
  // Move the User tab to the right.
165
  $items['commerce']['#weight'] = 5;
166 167 168 169 170
}

/**
 * @} End of "addtogroup hooks".
 */