2009-07-04 05:37:30 +00:00
<?php
/**
* @file
* Administration toolbar for quick access to top level administration items.
*/
2015-07-14 11:34:15 +00:00
use Drupal\Core\Cache\CacheableMetadata;
2014-03-31 17:37:55 +00:00
use Drupal\Core\Render\Element;
2014-06-30 03:33:08 +00:00
use Drupal\Core\Routing\RouteMatchInterface;
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
use Drupal\Core\Template\Attribute;
2013-05-02 04:46:53 +00:00
use Drupal\Component\Utility\Crypt;
2014-10-09 06:39:37 +00:00
use Drupal\Core\Url;
2019-07-05 07:47:35 +00:00
use Drupal\toolbar\Controller\ToolbarController;
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
2009-12-04 16:02:42 +00:00
/**
* Implements hook_help().
*/
2014-06-30 03:33:08 +00:00
function toolbar_help($route_name, RouteMatchInterface $route_match) {
2014-05-07 02:04:53 +00:00
switch ($route_name) {
case 'help.page.toolbar':
2009-12-04 16:02:42 +00:00
$output = '<h3>' . t('About') . '</h3>';
2017-03-04 01:20:24 +00:00
$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>';
2014-10-05 11:27:07 +00:00
$output .= '<h4>' . t('Terminology') . '</h4>';
2009-12-04 16:02:42 +00:00
$output .= '<dl>';
2014-10-05 11:27:07 +00:00
$output .= '<dt>' . t('Tabs') . '</dt>';
$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>';
$output .= '<dt>' . t('Trays') . '</dt>';
$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>';
2009-12-04 16:02:42 +00:00
$output .= '</dl>';
return $output;
}
}
2009-07-04 05:37:30 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_theme().
2009-07-04 05:37:30 +00:00
*/
function toolbar_theme($existing, $type, $theme, $path) {
2017-03-04 01:20:24 +00:00
$items['toolbar'] = [
2013-02-01 16:31:47 +00:00
'render element' => 'element',
2017-03-04 01:20:24 +00:00
];
$items['menu__toolbar'] = [
2015-05-24 22:17:43 +00:00
'base hook' => 'menu',
2017-03-04 01:20:24 +00:00
'variables' => ['items' => [], 'attributes' => []],
];
2014-01-08 08:40:53 +00:00
2009-11-15 21:13:26 +00:00
return $items;
}
2009-07-04 05:37:30 +00:00
/**
2014-10-16 12:36:06 +00:00
* Implements hook_page_top().
2009-11-10 17:27:54 +00:00
*
2014-10-16 12:36:06 +00:00
* Add admin toolbar to the top of the page automatically.
2009-07-04 05:37:30 +00:00
*/
2014-10-16 12:36:06 +00:00
function toolbar_page_top(array &$page_top) {
2017-03-04 01:20:24 +00:00
$page_top['toolbar'] = [
2013-02-01 16:31:47 +00:00
'#type' => 'toolbar',
Issue #2061977 by InternetDevels, kim.pepper, ianthomas_uk, herom, rhm50, naveenvalecha, andypost, mandar.harkare, sergeypavlenko, sidharthap, SIz, tkuldeep17: Replace user_access() calls with ->hasPermission() in all core modules except user.
2014-07-12 06:55:56 +00:00
'#access' => \Drupal::currentUser()->hasPermission('access toolbar'),
2015-06-01 16:49:02 +00:00
'#cache' => [
'keys' => ['toolbar'],
'contexts' => ['user.permissions'],
],
2017-03-04 01:20:24 +00:00
];
2009-10-26 04:45:10 +00:00
}
2013-02-01 16:31:47 +00:00
/**
2014-01-08 08:40:53 +00:00
* Prepares variables for administration toolbar templates.
*
* Default template: toolbar.html.twig.
2013-02-01 16:31:47 +00:00
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties and children of
* the tray. Properties used: #children, #attributes and #bar.
*/
2014-01-08 08:40:53 +00:00
function template_preprocess_toolbar(&$variables) {
$element = $variables['element'];
// Prepare the toolbar attributes.
$variables['attributes'] = $element['#attributes'];
$variables['toolbar_attributes'] = new Attribute($element['#bar']['#attributes']);
$variables['toolbar_heading'] = $element['#bar']['#heading'];
// Prepare the trays and tabs for each toolbar item as well as the remainder
// variable that will hold any non-tray, non-tab elements.
2017-03-04 01:20:24 +00:00
$variables['trays'] = [];
$variables['tabs'] = [];
$variables['remainder'] = [];
2014-03-31 17:37:55 +00:00
foreach (Element::children($element) as $key) {
2015-10-23 00:03:11 +00:00
// Early rendering to collect the wrapper attributes from
// ToolbarItem elements.
if (!empty($element[$key])) {
Drupal::service('renderer')->render($element[$key]);
}
2014-01-08 08:40:53 +00:00
// Add the tray.
if (isset($element[$key]['tray'])) {
2017-03-04 01:20:24 +00:00
$attributes = [];
2015-10-23 00:03:11 +00:00
if (!empty($element[$key]['tray']['#wrapper_attributes'])) {
$attributes = $element[$key]['tray']['#wrapper_attributes'];
}
2017-03-04 01:20:24 +00:00
$variables['trays'][$key] = [
2014-01-08 08:40:53 +00:00
'links' => $element[$key]['tray'],
2015-10-23 00:03:11 +00:00
'attributes' => new Attribute($attributes),
2017-03-04 01:20:24 +00:00
];
2014-01-08 08:40:53 +00:00
if (array_key_exists('#heading', $element[$key]['tray'])) {
$variables['trays'][$key]['label'] = $element[$key]['tray']['#heading'];
}
}
// Add the tab.
2015-06-03 02:34:52 +00:00
if (isset($element[$key]['tab'])) {
2017-03-04 01:20:24 +00:00
$attributes = [];
2015-06-01 16:49:02 +00:00
// Pass the wrapper attributes along.
2015-10-23 00:03:11 +00:00
if (!empty($element[$key]['#wrapper_attributes'])) {
2015-06-01 16:49:02 +00:00
$attributes = $element[$key]['#wrapper_attributes'];
}
2017-03-04 01:20:24 +00:00
$variables['tabs'][$key] = [
2015-06-01 16:49:02 +00:00
'link' => $element[$key]['tab'],
'attributes' => new Attribute($attributes),
2017-03-04 01:20:24 +00:00
];
2015-06-01 16:49:02 +00:00
}
2014-01-08 08:40:53 +00:00
// Add other non-tray, non-tab child elements to the remainder variable for
// later rendering.
2014-03-31 17:37:55 +00:00
foreach (Element::children($element[$key]) as $child_key) {
2017-03-04 01:20:24 +00:00
if (!in_array($child_key, ['tray', 'tab'])) {
2014-01-08 08:40:53 +00:00
$variables['remainder'][$key][$child_key] = $element[$key][$child_key];
}
2013-02-01 16:31:47 +00:00
}
}
2009-07-04 05:37:30 +00:00
}
/**
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
* Implements hook_toolbar().
2009-07-04 05:37:30 +00:00
*/
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
function toolbar_toolbar() {
// The 'Home' tab is a simple link, with no corresponding tray.
2017-03-04 01:20:24 +00:00
$items['home'] = [
2013-02-01 16:31:47 +00:00
'#type' => 'toolbar_item',
2017-03-04 01:20:24 +00:00
'tab' => [
2013-02-01 16:31:47 +00:00
'#type' => 'link',
2013-11-26 22:06:57 +00:00
'#title' => t('Back to site'),
2014-10-09 06:39:37 +00:00
'#url' => Url::fromRoute('<front>'),
2017-03-04 01:20:24 +00:00
'#attributes' => [
2014-03-25 21:05:55 +00:00
'title' => t('Return to site content'),
2017-03-04 01:20:24 +00:00
'class' => ['toolbar-icon', 'toolbar-icon-escape-admin'],
2014-03-25 21:05:55 +00:00
'data-toolbar-escape-admin' => TRUE,
2017-03-04 01:20:24 +00:00
],
],
'#wrapper_attributes' => [
Issue #2542050 by droplet, nod_, dpacassi, Wim Leers, Sam152, andrewmacpherson, tameeshb, jonathanshaw, lokapujya, Daniel Schaefer, Chi, geerlingguy, tarekdj: Toolbar implementation creates super annoying re-rendering
2017-06-19 16:37:48 +00:00
'class' => ['home-toolbar-tab'],
2017-03-04 01:20:24 +00:00
],
'#attached' => [
'library' => [
2014-03-09 19:59:45 +00:00
'toolbar/toolbar.escapeAdmin',
2017-03-04 01:20:24 +00:00
],
],
2013-02-01 16:31:47 +00:00
'#weight' => -20,
2017-03-04 01:20:24 +00:00
];
2009-07-04 05:37:30 +00:00
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
// To conserve bandwidth, we only include the top-level links in the HTML.
2013-09-18 13:24:42 +00:00
// The subtrees are fetched through a JSONP script that is generated at the
// toolbar_subtrees route. We provide the JavaScript requesting that JSONP
// script here with the hash parameter that is needed for that route.
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
// @see toolbar_subtrees_jsonp()
2015-07-14 11:34:15 +00:00
list($hash, $hash_cacheability) = _toolbar_get_subtrees_hash();
2014-12-04 11:37:23 +00:00
$subtrees_attached['drupalSettings']['toolbar'] = [
2015-07-14 11:34:15 +00:00
'subtreesHash' => $hash,
2014-12-04 11:37:23 +00:00
];
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
2013-02-01 16:31:47 +00:00
// The administration element has a link that is themed to correspond to
// a toolbar tray. The tray contains the full administrative menu of the site.
2017-03-04 01:20:24 +00:00
$items['administration'] = [
2013-02-01 16:31:47 +00:00
'#type' => 'toolbar_item',
2017-03-04 01:20:24 +00:00
'tab' => [
2013-02-01 16:31:47 +00:00
'#type' => 'link',
2014-01-10 09:54:46 +00:00
'#title' => t('Manage'),
2014-10-09 06:39:37 +00:00
'#url' => Url::fromRoute('system.admin'),
2017-03-04 01:20:24 +00:00
'#attributes' => [
2014-03-25 21:05:55 +00:00
'title' => t('Admin menu'),
2017-03-04 01:20:24 +00:00
'class' => ['toolbar-icon', 'toolbar-icon-menu'],
2014-03-25 21:05:55 +00:00
// A data attribute that indicates to the client to defer loading of
// the admin menu subtrees until this tab is activated. Admin menu
// subtrees will not render to the DOM if this attribute is removed.
// The value of the attribute is intentionally left blank. Only the
// presence of the attribute is necessary.
'data-drupal-subtrees' => '',
2017-03-04 01:20:24 +00:00
],
],
'tray' => [
2014-07-30 12:04:04 +00:00
'#heading' => t('Administration menu'),
'#attached' => $subtrees_attached,
2017-03-04 01:20:24 +00:00
'toolbar_administration' => [
2019-07-05 07:47:35 +00:00
'#pre_render' => [[ToolbarController::class, 'preRenderAdministrationTray']],
2014-07-30 12:04:04 +00:00
'#type' => 'container',
2017-03-04 01:20:24 +00:00
'#attributes' => [
'class' => ['toolbar-menu-administration'],
],
],
],
2013-02-01 16:31:47 +00:00
'#weight' => -15,
2017-03-04 01:20:24 +00:00
];
2015-07-14 11:34:15 +00:00
$hash_cacheability->applyTo($items['administration']);
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
return $items;
}
2009-07-04 05:37:30 +00:00
/**
2014-07-30 12:04:04 +00:00
* Renders the toolbar's administration tray.
2011-12-19 10:59:44 +00:00
*
2014-07-30 12:04:04 +00:00
* @param array $element
* A renderable array.
*
* @return array
* The updated renderable array.
*
2019-07-05 07:47:35 +00:00
* @deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use
* \Drupal\toolbar\Controller\ToolbarController::preRenderAdministrationTray()
* instead.
*
* @see https://www.drupal.org/node/2966725
Issue #2938970 by MerryHamster, surbz, msankhala, alexpott, Jo Fitzgerald, Yasiru Nilan, Manuel Garcia, ZeiP, boaloysius, xjm, wturrell, tameeshb, darrenwh, ritzz: Replace drupal_render() in @param, @return, @see, @link, etc
2018-05-03 16:58:24 +00:00
* @see \Drupal\Core\Render\RendererInterface::render()
2009-07-04 05:37:30 +00:00
*/
2014-07-30 12:04:04 +00:00
function toolbar_prerender_toolbar_administration_tray(array $element) {
2019-07-05 07:47:35 +00:00
@trigger_error('toolbar_prerender_toolbar_administration_tray() is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use \Drupal\toolbar\Controller\ToolbarController::preRenderAdministrationTray() instead. See https://www.drupal.org/node/2966725', E_USER_DEPRECATED);
return ToolbarController::preRenderAdministrationTray($element);
2009-07-04 05:37:30 +00:00
}
/**
2014-07-30 12:04:04 +00:00
* Adds toolbar-specific attributes to the menu link tree.
2009-07-04 05:37:30 +00:00
*
2014-07-30 12:04:04 +00:00
* @param \Drupal\Core\Menu\MenuLinkTreeElement[] $tree
* The menu link tree to manipulate.
2011-12-19 10:59:44 +00:00
*
2014-07-30 12:04:04 +00:00
* @return \Drupal\Core\Menu\MenuLinkTreeElement[]
* The manipulated menu link tree.
2009-07-04 05:37:30 +00:00
*/
2014-07-30 12:04:04 +00:00
function toolbar_menu_navigation_links(array $tree) {
foreach ($tree as $element) {
if ($element->subtree) {
toolbar_menu_navigation_links($element->subtree);
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
}
2014-07-30 12:04:04 +00:00
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
// Make sure we have a path specific ID in place, so we can attach icons
2014-07-30 12:04:04 +00:00
// and behaviors to the menu links.
$link = $element->link;
$url = $link->getUrlObject();
2015-07-17 22:27:09 +00:00
if (!$url->isRouted()) {
2014-07-30 12:04:04 +00:00
// This is an unusual case, so just get a distinct, safe string.
2014-09-30 06:50:14 +00:00
$id = substr(Crypt::hashBase64($url->getUri()), 0, 16);
2014-07-30 12:04:04 +00:00
}
else {
2017-03-04 01:20:24 +00:00
$id = str_replace(['.', '<', '>'], ['-', '', ''], $url->getRouteName());
2014-07-30 12:04:04 +00:00
}
// Get the non-localized title to make the icon class.
$definition = $link->getPluginDefinition();
$element->options['attributes']['id'] = 'toolbar-link-' . $id;
$element->options['attributes']['class'][] = 'toolbar-icon';
2017-03-04 01:20:24 +00:00
$element->options['attributes']['class'][] = 'toolbar-icon-' . strtolower(str_replace(['.', ' ', '_'], ['-', '-', '-'], $definition['id']));
2015-08-27 14:42:23 +00:00
$element->options['attributes']['title'] = $link->getDescription();
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
}
2014-07-30 12:04:04 +00:00
return $tree;
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
}
2009-07-04 05:37:30 +00:00
Issue #2542050 by droplet, nod_, dpacassi, Wim Leers, Sam152, andrewmacpherson, tameeshb, jonathanshaw, lokapujya, Daniel Schaefer, Chi, geerlingguy, tarekdj: Toolbar implementation creates super annoying re-rendering
2017-06-19 16:37:48 +00:00
/**
* Implements hook_preprocess_HOOK() for HTML document templates.
*/
function toolbar_preprocess_html(&$variables) {
if (!\Drupal::currentUser()->hasPermission('access toolbar')) {
return;
}
2019-02-22 12:17:02 +00:00
$variables['attributes']['class'][] = 'toolbar-tray-open';
$variables['attributes']['class'][] = 'toolbar-horizontal';
$variables['attributes']['class'][] = 'toolbar-fixed';
$variables['attributes']['class'][] = 'toolbar-loading';
Issue #2542050 by droplet, nod_, dpacassi, Wim Leers, Sam152, andrewmacpherson, tameeshb, jonathanshaw, lokapujya, Daniel Schaefer, Chi, geerlingguy, tarekdj: Toolbar implementation creates super annoying re-rendering
2017-06-19 16:37:48 +00:00
}
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
/**
* Returns the rendered subtree of each top-level toolbar link.
2015-07-14 11:34:15 +00:00
*
* @return array
* An array with the following key-value pairs:
* - 'subtrees': the rendered subtrees
* - 'cacheability: the associated cacheability.
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
*/
function toolbar_get_rendered_subtrees() {
2015-07-14 11:34:15 +00:00
$data = [
2019-07-05 07:47:35 +00:00
'#pre_render' => [[ToolbarController::class, 'preRenderGetRenderedSubtrees']],
2015-07-14 11:34:15 +00:00
'#cache' => [
'keys' => [
'toolbar_rendered_subtrees',
],
],
'#cache_properties' => ['#subtrees'],
];
\Drupal::service('renderer')->renderPlain($data);
return [$data['#subtrees'], CacheableMetadata::createFromRenderArray($data)];
}
/**
* #pre_render callback for toolbar_get_rendered_subtrees().
2019-07-05 07:47:35 +00:00
*
* @deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use
* \Drupal\toolbar\Controller\ToolbarController::preRenderGetRenderedSubtrees()
* instead.
*
* @see https://www.drupal.org/node/2966725
2015-07-14 11:34:15 +00:00
*/
function _toolbar_do_get_rendered_subtrees(array $data) {
2019-07-05 07:47:35 +00:00
@trigger_error('_toolbar_do_get_rendered_subtrees() is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use Drupal\toolbar\Controller\ToolbarController::preRenderGetRenderedSubtrees() instead. See https://www.drupal.org/node/2966725', E_USER_DEPRECATED);
return ToolbarController::preRenderGetRenderedSubtrees($data);
2009-07-04 05:37:30 +00:00
}
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
/**
* Returns the hash of the per-user rendered toolbar subtrees.
2013-09-18 13:24:42 +00:00
*
* @return string
* The hash of the admin_menu subtrees.
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
*/
2015-07-14 11:34:15 +00:00
function _toolbar_get_subtrees_hash() {
list($subtrees, $cacheability) = toolbar_get_rendered_subtrees();
$hash = Crypt::hashBase64(serialize($subtrees));
return [$hash, $cacheability];
Issue #1137920 by jessebeach, lewisnyman, tkoleary, Bojhan, webchick, benjifisher, nod_, sjbassett, kathryn531, effulgentsia, Everett Zufelt: fixed toolbar on small screen sizes and redesign toolbar for desktop.
2012-11-21 17:18:57 +00:00
}