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-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-07-30 12:02:58 +00:00
use Drupal\menu_link\MenuLinkInterface;
2013-09-18 13:24:42 +00:00
use Drupal\user\RoleInterface;
use Drupal\user\UserInterface;
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>';
2012-03-21 05:51:30 +00:00
$output .= '<p>' . t('The Toolbar module displays links to top-level administration menu items and links from other modules at the top of the screen. For more information, see the online handbook entry for <a href="@toolbar">Toolbar module</a>.', array('@toolbar' => 'http://drupal.org/documentation/modules/toolbar')) . '</p>';
2009-12-04 16:02:42 +00:00
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Displaying administrative links') . '</dt>';
2013-02-01 16:31:47 +00:00
$output .= '<dd>' . t('The Toolbar module displays a bar containing top-level administrative components across the top of the screen. Below that, the Toolbar module has a <em>drawer</em> section where it displays links provided by other modules, such as the core <a href="@shortcuts-help">Shortcut module</a>. The drawer can be hidden/shown by clicking on its corresponding tab.', array('@shortcuts-help' => url('admin/help/shortcut'))) . '</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_permission().
2009-07-04 05:37:30 +00:00
*/
2009-07-05 18:00:11 +00:00
function toolbar_permission() {
2009-07-04 05:37:30 +00:00
return array(
'access toolbar' => array(
2009-12-01 13:14:43 +00:00
'title' => t('Use the administration toolbar'),
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
'template' => 'toolbar',
2013-02-01 16:31:47 +00:00
);
$items['toolbar_item'] = array(
'render element' => 'element',
);
2014-01-08 08:40:53 +00:00
2009-11-15 21:13:26 +00:00
return $items;
}
2013-02-01 16:31:47 +00:00
/**
* Implements hook_element_info().
*/
function toolbar_element_info() {
$elements = array();
$elements['toolbar'] = array(
'#pre_render' => array('toolbar_pre_render'),
'#theme' => 'toolbar',
'#attached' => array(
'library' => array(
2014-03-09 19:59:45 +00:00
'toolbar/toolbar',
2013-02-01 16:31:47 +00:00
),
),
// Metadata for the toolbar wrapping element.
'#attributes' => array(
// The id cannot be simply "toolbar" or it will clash with the simpletest
// tests listing which produces a checkbox with attribute id="toolbar"
'id' => 'toolbar-administration',
2013-08-28 08:49:00 +00:00
'class' => array('toolbar'),
2014-07-24 13:23:04 +00:00
'role' => 'group',
'aria-label' => t('Site administration toolbar'),
2013-02-01 16:31:47 +00:00
),
// Metadata for the administration bar.
'#bar' => array(
'#heading' => t('Toolbar items'),
'#attributes' => array(
'id' => 'toolbar-bar',
2014-07-24 13:23:04 +00:00
'class' => array('toolbar-bar', 'clearfix',),
'role' => 'navigation',
'aria-label' => t('Toolbar items'),
2013-02-01 16:31:47 +00:00
),
),
);
// A toolbar item is wrapped in markup for common styling. The 'tray'
2014-01-08 08:40:53 +00:00
// property contains a renderable array.
2013-02-01 16:31:47 +00:00
$elements['toolbar_item'] = array(
'#pre_render' => array('toolbar_pre_render_item'),
'#theme' => 'toolbar_item',
'tab' => array(
'#type' => 'link',
'#title' => NULL,
'#href' => '',
),
);
return $elements;
}
2009-11-15 21:13:26 +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
* Use Drupal's page cache for toolbar/subtrees/*, even for authenticated users.
2010-01-30 07:59:26 +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
* This gets invoked after full bootstrap, so must duplicate some of what's
2014-06-26 10:47:01 +00:00
* done by \Drupal\Core\DrupalKernel::handlePageCache().
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
*
* @todo Replace this hack with something better integrated with DrupalKernel
* once Drupal's page caching itself is properly integrated.
2009-11-15 21:13:26 +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_initialize_page_cache() {
$GLOBALS['conf']['system.performance']['cache']['page']['enabled'] = TRUE;
drupal_page_is_cacheable(TRUE);
// If we have a cache, serve it.
2014-06-26 10:47:01 +00:00
// @see \Drupal\Core\DrupalKernel::handlePageCache()
2013-08-12 01:58:00 +00:00
$request = \Drupal::request();
2014-05-09 09:52:16 +00:00
$response = drupal_page_get_cache($request);
if ($response) {
2013-06-06 08:21:58 +00:00
$response->headers->set('X-Drupal-Cache', 'HIT');
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-05-09 09:52:16 +00:00
drupal_serve_page_from_cache($response, $request);
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-06-06 08:21:58 +00:00
$response->prepare($request);
$response->send();
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
// We are done.
exit;
}
// The Expires HTTP header is the heart of the client-side HTTP caching. The
// additional server-side page cache only takes effect when the client
// accesses the callback URL again (e.g., after clearing the browser cache or
// when force-reloading a Drupal page).
$max_age = 3600 * 24 * 365;
2014-07-07 15:33:35 +00:00
drupal_add_http_header('Expires', gmdate(DateTimePlus::RFC7231, REQUEST_TIME + $max_age));
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
drupal_add_http_header('Cache-Control', 'private, max-age=' . $max_age);
2009-11-15 21:13:26 +00:00
}
2009-07-04 05:37:30 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_page_build().
2009-11-10 17:27:54 +00:00
*
2009-07-04 05:37:30 +00:00
* Add admin toolbar to the page_top region automatically.
*/
2009-08-31 16:46:32 +00:00
function toolbar_page_build(&$page) {
2009-10-26 04:45:10 +00:00
$page['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
* Builds the Toolbar as a structured array ready for drupal_render().
2009-10-26 04:45:10 +00:00
*
* Since building the toolbar takes some time, it is done just prior to
* rendering to ensure that it is built only if it will be displayed.
2011-12-19 10:59:44 +00:00
*
2013-02-01 16:31:47 +00:00
* @param array $element
* A renderable array.
*
* @return
* A renderable array.
*
2011-12-19 10:59:44 +00:00
* @see toolbar_page_build().
2009-10-26 04:45:10 +00:00
*/
2013-02-01 16:31:47 +00:00
function toolbar_pre_render($element) {
// Get the configured breakpoints to switch from vertical to horizontal
// toolbar presentation.
$breakpoints = entity_load('breakpoint_group', 'module.toolbar.toolbar');
if (!empty($breakpoints)) {
$media_queries = array();
$media_queries['toolbar']['breakpoints'] = array_map(
function ($object) {
return $object->mediaQuery;
},
2013-11-20 02:59:45 +00:00
$breakpoints->getBreakpoints()
2013-02-01 16:31:47 +00:00
);
$element['#attached']['js'][] = array(
'data' => $media_queries,
'type' => 'setting',
);
}
// Get toolbar items from all modules that implement hook_toolbar().
2013-09-16 03:58:06 +00:00
$items = \Drupal::moduleHandler()->invokeAll('toolbar');
2013-02-01 16:31:47 +00:00
// Allow for altering of hook_toolbar().
2014-02-24 10:10:52 +00:00
\Drupal::moduleHandler()->alter('toolbar', $items);
2013-02-01 16:31:47 +00:00
// Sort the children.
2014-03-24 14:44:46 +00:00
uasort($items, array('\Drupal\Component\Utility\SortArray', 'sortByWeightProperty'));
2013-02-01 16:31:47 +00:00
// Merge in the original toolbar values.
$element = array_merge($element, $items);
// Render the children.
$element['#children'] = drupal_render_children($element);
return $element;
}
/**
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'];
}
}
// Pass the wrapper attributes along.
if (array_key_exists('#wrapper_attributes', $element[$key])) {
$element[$key]['#wrapper_attributes']['class'][] = 'toolbar-tab';
$attributes = $element[$key]['#wrapper_attributes'];
}
else {
$attributes = array('class' => array('toolbar-tab'));
}
// 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
}
/**
2013-02-01 16:31:47 +00:00
* Provides markup for associating a tray trigger with a tray element.
*
* A tray is a responsive container that wraps renderable content. Trays present
* content well on small and large screens alike.
*
* @param array $element
* A renderable array.
*
* @return
* A renderable array.
2009-07-04 05:37:30 +00:00
*/
2013-02-01 16:31:47 +00:00
function toolbar_pre_render_item($element) {
2013-07-28 23:10:43 +00:00
// Assign each item a unique ID.
$id = drupal_html_id('toolbar-item');
// Provide attributes for a toolbar item.
$attributes = array(
'id' => $id,
);
2013-02-01 16:31:47 +00:00
// If tray content is present, markup the tray and its associated trigger.
if (!empty($element['tray'])) {
2013-07-28 23:10:43 +00:00
// Provide attributes necessary for trays.
$attributes += array(
'data-toolbar-tray' => $id . '-tray',
2013-02-01 16:31:47 +00:00
'aria-owns' => $id,
'role' => 'button',
'aria-pressed' => 'false',
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-07-28 23:10:43 +00:00
2013-02-01 16:31:47 +00:00
// Merge in module-provided attributes.
$element['tab'] += array('#attributes' => array());
$element['tab']['#attributes'] += $attributes;
$element['tab']['#attributes']['class'][] = 'trigger';
// Provide attributes for the tray theme wrapper.
$attributes = array(
2013-07-28 23:10:43 +00:00
'id' => $id . '-tray',
'data-toolbar-tray' => $id . '-tray',
'aria-owned-by' => $id,
2013-02-01 16:31:47 +00:00
);
// Merge in module-provided attributes.
if (!isset($element['tray']['#wrapper_attributes'])) {
$element['tray']['#wrapper_attributes'] = array();
}
$element['tray']['#wrapper_attributes'] += $attributes;
2013-07-18 10:28:54 +00:00
$element['tray']['#wrapper_attributes']['class'][] = 'toolbar-tray';
2013-02-01 16:31:47 +00:00
}
2014-03-25 21:05:55 +00:00
$element['tab']['#attributes']['class'][] = 'toolbar-item';
2013-02-01 16:31:47 +00:00
return $element;
}
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() {
2014-07-30 12:02:58 +00:00
$items = array();
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
// 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'),
2013-02-01 16:31:47 +00:00
'#href' => '<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
);
2014-07-30 12:02:58 +00:00
// Retrieve the administration menu from the database.
$tree = toolbar_get_menu_tree();
// Add attributes to the links before rendering.
toolbar_menu_navigation_links($tree);
/** @var \Drupal\menu_link\MenuTreeInterface $menu_tree */
$menu_tree = \Drupal::service('menu_link.tree');
$menu = array(
'#heading' => t('Administration menu'),
'toolbar_administration' => array(
'#type' => 'container',
'#attributes' => array(
'class' => array('toolbar-menu-administration'),
),
'administration_menu' => $menu_tree->renderTree($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
// 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-04-03 15:13:13 +00:00
$langcode = \Drupal::languageManager()->getCurrentLanguage()->id;
2014-07-30 12:02:58 +00:00
$menu['toolbar_administration']['#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'),
2013-02-01 16:31:47 +00:00
'#href' => '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:02:58 +00:00
'tray' => $menu,
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:02:58 +00:00
* Gets only the top level items below the 'admin' path.
2011-12-19 10:59:44 +00:00
*
2014-07-30 12:02:58 +00:00
* @return
* An array containing a menu tree of top level items below the 'admin' path.
2009-07-04 05:37:30 +00:00
*/
2014-07-30 12:02:58 +00:00
function toolbar_get_menu_tree() {
$tree = array();
/** @var \Drupal\menu_link\MenuTreeInterface $menu_tree */
$menu_tree = \Drupal::service('menu_link.tree');
$query = \Drupal::entityQuery('menu_link')
->condition('menu_name', 'admin')
->condition('module', 'system')
->condition('link_path', 'admin');
$result = $query->execute();
if (!empty($result)) {
$admin_link = menu_link_load(reset($result));
$tree = $menu_tree->buildTree('admin', array(
'expanded' => array($admin_link['mlid']),
'min_depth' => $admin_link['depth'] + 1,
'max_depth' => $admin_link['depth'] + 1,
));
}
return $tree;
2009-07-04 05:37:30 +00:00
}
/**
2014-07-30 12:02:58 +00:00
* Generates an array of links from a menu tree array.
2009-07-04 05:37:30 +00:00
*
2014-07-30 12:02:58 +00:00
* Based on menu_navigation_links(). Adds path based IDs and icon placeholders
* to the links.
2011-12-19 10:59:44 +00:00
*
2014-07-30 12:02:58 +00:00
* @return
* An array of links as defined above.
2009-07-04 05:37:30 +00:00
*/
2014-07-30 12:02:58 +00:00
function toolbar_menu_navigation_links(&$tree) {
foreach ($tree as $key => $item) {
// Configure sub-items.
if (!empty($item['below'])) {
toolbar_menu_navigation_links($tree[$key]['below']);
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:02:58 +00:00
// and behaviors to the items.
$tree[$key]['link']['localized_options']['attributes'] = array(
'id' => 'toolbar-link-' . str_replace(array('/', '<', '>'), array('-', '', ''), $item['link']['link_path']),
'class' => array(
'toolbar-icon',
'toolbar-icon-' . strtolower(str_replace(' ', '-', $item['link']['link_title'])),
),
'title' => String::checkPlain($item['link']['description']),
);
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() {
$subtrees = array();
2014-07-30 12:02:58 +00:00
/** @var \Drupal\menu_link\MenuTreeInterface $menu_tree */
$menu_tree = \Drupal::service('menu_link.tree');
$tree = toolbar_get_menu_tree();
foreach ($tree as $tree_item) {
$item = $tree_item['link'];
if (!$item['hidden'] && $item['access']) {
if ($item['has_children']) {
$query = \Drupal::entityQuery('menu_link')
->condition('has_children', 1);
for ($i=1; $i <= $item['depth']; $i++) {
$query->condition('p' . $i, $item['p' . $i]);
}
$parents = $query->execute();
$subtree = $menu_tree->buildTree($item['menu_name'], array('expanded' => $parents, 'min_depth' => $item['depth']+1));
toolbar_menu_navigation_links($subtree);
$subtree = $menu_tree->renderTree($subtree);
$subtree = drupal_render($subtree);
}
else {
$subtree = '';
}
2014-07-30 10:47:01 +00:00
2014-07-30 12:02:58 +00:00
$id = str_replace(array('/', '<', '>'), array('-', '', ''), $item['link_path']);
$subtrees[$id] = $subtree;
}
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-07-30 12:02:58 +00:00
\Drupal::cache('toolbar')->set($cid, $hash, Cache::PERMANENT, array('user' => array($uid), 'locale' => TRUE,));
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
/**
2013-09-20 01:01:16 +00:00
* Implements hook_modules_installed().
2013-09-18 13:24:42 +00:00
*/
2013-09-20 01:01:16 +00:00
function toolbar_modules_installed($modules) {
2013-09-18 13:24:42 +00:00
_toolbar_clear_user_cache();
}
/**
2013-09-20 01:01:16 +00:00
* Implements hook_modules_uninstalled().
2013-09-18 13:24:42 +00:00
*/
2013-09-20 01:01:16 +00:00
function toolbar_modules_uninstalled($modules) {
2013-09-18 13:24:42 +00:00
_toolbar_clear_user_cache();
}
2014-07-30 12:02:58 +00:00
/**
* Implements hook_ENTITY_TYPE_update() for menu_link entities.
*/
function toolbar_menu_link_update(MenuLinkInterface $menu_link) {
if ($menu_link->menu_name === 'admin') {
_toolbar_clear_user_cache();
}
}
2013-09-18 13:24:42 +00:00
/**
2014-07-11 12:04:53 +00:00
* Implements hook_ENTITY_TYPE_update() for user entities.
2013-09-18 13:24:42 +00:00
*/
function toolbar_user_update(UserInterface $user) {
_toolbar_clear_user_cache($user->id());
}
/**
2014-07-11 12:04:53 +00:00
* Implements hook_ENTITY_TYPE_update() for user_role entities.
2013-09-18 13:24:42 +00:00
*/
function toolbar_user_role_update(RoleInterface $role) {
_toolbar_clear_user_cache();
}
/**
* 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
}
/**
* Clears the Toolbar user cache.
*
* @param int $uid
* (optional) The user ID whose toolbar cache entry to clear.
*/
function _toolbar_clear_user_cache($uid = NULL) {
2014-05-09 13:57:55 +00:00
// Clear by the 'user' tag in order to delete all caches, in any language,
// associated with this user.
if (isset($uid)) {
Cache::deleteTags(array('user' => array($uid)));
} else {
\Drupal::cache('toolbar')->deleteAll();
2013-09-18 13:24:42 +00:00
}
}