2009-07-04 05:37:30 +00:00
<?php
/**
* @file
* Administration toolbar for quick access to top level administration items.
*/
2014-01-22 20:35:48 +00:00
use Drupal\Core\Cache\Cache;
2014-07-30 12:04:04 +00:00
use Drupal\Core\Menu\MenuTreeParameters;
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;
2014-07-07 15:33:35 +00:00
use Drupal\Component\Datetime\DateTimePlus;
2013-05-02 04:46:53 +00:00
use Drupal\Component\Utility\Crypt;
2014-07-07 15:33:35 +00:00
use Drupal\Component\Utility\String;
2014-10-01 08:20:51 +00:00
use Drupal\user\Entity\Role;
2014-10-09 06:39:37 +00:00
use Drupal\Core\Url;
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>';
2014-10-05 11:27:07 +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>.', array('!toolbar_docs' => 'https://drupal.org/documentation/modules/toolbar')) . '</p>';
$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) {
$items['toolbar'] = array(
2013-02-01 16:31:47 +00:00
'render element' => 'element',
);
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) {
$page_top['toolbar'] = array(
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'),
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.
$variables['trays'] = array();
$variables['tabs'] = array();
$variables['remainder'] = array();
2014-03-31 17:37:55 +00:00
foreach (Element::children($element) as $key) {
2014-01-08 08:40:53 +00:00
// Add the tray.
if (isset($element[$key]['tray'])) {
$variables['trays'][$key] = array(
'links' => $element[$key]['tray'],
'attributes' => new Attribute($element[$key]['tray']['#wrapper_attributes']),
);
if (array_key_exists('#heading', $element[$key]['tray'])) {
$variables['trays'][$key]['label'] = $element[$key]['tray']['#heading'];
}
}
2014-09-08 18:32:55 +00:00
$attributes = array();
2014-01-08 08:40:53 +00:00
// Pass the wrapper attributes along.
if (array_key_exists('#wrapper_attributes', $element[$key])) {
$attributes = $element[$key]['#wrapper_attributes'];
}
// Add the tab.
$variables['tabs'][$key] = array(
'link' => $element[$key]['tab'],
'attributes' => new Attribute($attributes),
);
// 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) {
2014-01-08 08:40:53 +00:00
if (!in_array($child_key, array('tray', 'tab'))) {
$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.
$items['home'] = array(
2013-02-01 16:31:47 +00:00
'#type' => 'toolbar_item',
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
'tab' => array(
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>'),
2014-03-25 21:05:55 +00:00
'#attributes' => array(
'title' => t('Return to site content'),
'class' => array('toolbar-icon', 'toolbar-icon-escape-admin'),
'data-toolbar-escape-admin' => TRUE,
2009-09-05 15:05:05 +00:00
),
2009-07-04 05:37:30 +00:00
),
2013-11-26 22:06:57 +00:00
'#wrapper_attributes' => array(
'class' => array('hidden'),
),
'#attached' => array(
'library' => array(
2014-03-09 19:59:45 +00:00
'toolbar/toolbar.escapeAdmin',
2013-11-26 22:06:57 +00:00
),
),
2013-02-01 16:31:47 +00:00
'#weight' => -20,
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()
2014-10-13 09:10:32 +00:00
$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
2014-07-30 12:04:04 +00:00
$subtrees_attached['js'][] = array(
2013-09-18 13:24:42 +00:00
'type' => 'setting',
'data' => array('toolbar' => array(
2014-04-03 15:13:13 +00:00
'subtreesHash' => _toolbar_get_subtrees_hash($langcode),
'langcode' => $langcode,
2013-09-18 13:24:42 +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.
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
$items['administration'] = array(
2013-02-01 16:31:47 +00:00
'#type' => 'toolbar_item',
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
'tab' => array(
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'),
2014-03-25 21:05:55 +00:00
'#attributes' => array(
'title' => t('Admin menu'),
'class' => array('toolbar-icon', 'toolbar-icon-menu'),
// 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' => '',
2009-10-26 04:45:10 +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
),
2014-07-30 12:04:04 +00:00
'tray' => array(
'#heading' => t('Administration menu'),
'#attached' => $subtrees_attached,
'toolbar_administration' => array(
'#pre_render' => array(
'toolbar_prerender_toolbar_administration_tray',
),
'#type' => 'container',
'#attributes' => array(
'class' => array('toolbar-menu-administration'),
),
),
),
2013-02-01 16:31:47 +00:00
'#weight' => -15,
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.
*
* @see drupal_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) {
$menu_tree = \Drupal::menuTree();
// Render the top-level administration menu links.
$parameters = new MenuTreeParameters();
2014-08-12 19:30:35 +00:00
$parameters->setRoot('system.admin')->excludeRoot()->setTopLevelOnly()->onlyEnabledLinks();
2014-07-30 12:04:04 +00:00
$tree = $menu_tree->load(NULL, $parameters);
$manipulators = array(
array('callable' => 'menu.default_tree_manipulators:checkAccess'),
array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
array('callable' => 'toolbar_menu_navigation_links'),
);
$tree = $menu_tree->transform($tree, $manipulators);
$element['administration_menu'] = $menu_tree->build($tree);
return $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();
if ($url->isExternal()) {
// 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 {
$id = str_replace(array('.', '<', '>'), array('-', '', ''), $url->getRouteName());
}
// 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';
2014-08-20 15:00:09 +00:00
$element->options['attributes']['class'][] = 'toolbar-icon-' . strtolower(str_replace(array('.', ' ', '_'), array('-', '-', '-'), $definition['id']));
2014-07-30 12:04:04 +00:00
$element->options['attributes']['title'] = String::checkPlain($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 #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.
*/
function toolbar_get_rendered_subtrees() {
2014-07-30 12:04:04 +00:00
$menu_tree = \Drupal::menuTree();
$parameters = new MenuTreeParameters();
2014-08-12 19:30:35 +00:00
$parameters->setRoot('system.admin')->excludeRoot()->setMaxDepth(3)->onlyEnabledLinks();
2014-07-30 12:04:04 +00:00
$tree = $menu_tree->load(NULL, $parameters);
$manipulators = array(
array('callable' => 'menu.default_tree_manipulators:checkAccess'),
array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
array('callable' => 'toolbar_menu_navigation_links'),
);
$tree = $menu_tree->transform($tree, $manipulators);
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
$subtrees = array();
2014-07-30 12:04:04 +00:00
foreach ($tree as $element) {
/** @var \Drupal\Core\Menu\MenuLinkInterface $link */
$link = $element->link;
if ($element->subtree) {
$subtree = $menu_tree->build($element->subtree);
$output = drupal_render($subtree);
2014-07-30 12:02:58 +00:00
}
2014-07-30 12:04:04 +00:00
else {
$output = '';
}
// Many routes have dots as route name, while some special ones like <front>
// have <> characters in them.
$id = str_replace(array('.', '<', '>'), array('-', '', '' ), $link->getUrlObject()->getRouteName());
$subtrees[$id] = $output;
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
return $subtrees;
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
*
2014-04-03 15:13:13 +00:00
* @param string $langcode
* The langcode of the current request.
*
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
*/
2014-04-03 15:13:13 +00:00
function _toolbar_get_subtrees_hash($langcode) {
2013-09-18 13:24:42 +00:00
$uid = \Drupal::currentUser()->id();
2014-04-03 15:13:13 +00:00
$cid = _toolbar_get_user_cid($uid, $langcode);
2014-02-21 15:21:08 +00:00
if ($cache = \Drupal::cache('toolbar')->get($cid)) {
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
$hash = $cache->data;
}
else {
$subtrees = toolbar_get_rendered_subtrees();
2013-05-02 04:46:53 +00:00
$hash = Crypt::hashBase64(serialize($subtrees));
2013-09-18 13:24:42 +00:00
// Cache using a tag 'user' so that we can invalidate all user-specific
// caches later, based on the user's ID regardless of language.
// Clear the cache when the 'locale' tag is deleted. This ensures a fresh
// subtrees rendering when string translations are made.
2014-10-01 08:20:51 +00:00
$role_list_cache_tags = \Drupal::entityManager()->getDefinition('user_role')->getListCacheTags();
\Drupal::cache('toolbar')->set($cid, $hash, Cache::PERMANENT, Cache::mergeTags(array('user:' . $uid, 'locale', 'menu:admin'), $role_list_cache_tags));
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 $hash;
}
2013-09-18 13:24:42 +00:00
/**
* Returns a cache ID from the user and language IDs.
*
* @param int $uid
* A user ID.
2014-04-03 15:13:13 +00:00
* @param string $langcode
* The langcode of the current request.
2013-09-18 13:24:42 +00:00
*
* @return string
* A unique cache ID for the user.
*/
2014-04-03 15:13:13 +00:00
function _toolbar_get_user_cid($uid, $langcode) {
return 'toolbar_' . $uid . ':' . $langcode;
2013-09-18 13:24:42 +00:00
}