toolbar.module
File
-
core/
modules/ toolbar/ toolbar.module
View source
<?php
/**
* @file
*/
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Render\RenderContext;
use Drupal\Component\Utility\Crypt;
use Drupal\toolbar\Controller\ToolbarController;
use Drupal\toolbar\Hook\ToolbarThemeHooks;
/**
* Prepares variables for administration toolbar templates.
*
* Default template: toolbar.html.twig.
*
* @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.
*
* @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial
* template_preprocess functions are registered directly in hook_theme().
*
* @see https://www.drupal.org/node/3504125
*/
function template_preprocess_toolbar(&$variables) : void {
@trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
\Drupal::service(ToolbarThemeHooks::class)->preprocessToolbar($variables);
}
/**
* Adds toolbar-specific attributes to the menu link tree.
*
* @param \Drupal\Core\Menu\MenuLinkTreeElement[] $tree
* The menu link tree to manipulate.
*
* @return \Drupal\Core\Menu\MenuLinkTreeElement[]
* The manipulated menu link tree.
*/
function toolbar_menu_navigation_links(array $tree) {
foreach ($tree as $element) {
if ($element->subtree) {
toolbar_menu_navigation_links($element->subtree);
}
// Make sure we have a path specific ID in place, so we can attach icons
// and behaviors to the menu links.
$link = $element->link;
$url = $link->getUrlObject();
if (!$url->isRouted()) {
// This is an unusual case, so just get a distinct, safe string.
$id = substr(Crypt::hashBase64($url->getUri()), 0, 16);
}
else {
$id = str_replace([
'.',
'<',
'>',
], [
'-',
'',
'',
], $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';
$element->options['attributes']['class'][] = 'toolbar-icon-' . strtolower(str_replace([
'.',
' ',
'_',
], [
'-',
'-',
'-',
], $definition['id']));
$element->options['attributes']['title'] = $link->getDescription();
}
return $tree;
}
/**
* Returns the rendered subtree of each top-level toolbar link.
*
* @return array
* An array with the following key-value pairs:
* - 'subtrees': the rendered subtrees
* - 'cacheability: the associated cacheability.
*/
function toolbar_get_rendered_subtrees() {
$data = [
'#pre_render' => [
[
ToolbarController::class,
'preRenderGetRenderedSubtrees',
],
],
'#cache' => [
'keys' => [
'toolbar_rendered_subtrees',
],
],
'#cache_properties' => [
'#subtrees',
],
];
/** @var \Drupal\Core\Render\Renderer $renderer */
$renderer = \Drupal::service('renderer');
// The pre_render process populates $data during the render pipeline.
// We need to pass by reference so that populated data can be returned and
// used to resolve cacheability.
$renderer->executeInRenderContext(new RenderContext(), function () use ($renderer, &$data) {
$renderer->render($data);
});
return [
$data['#subtrees'],
CacheableMetadata::createFromRenderArray($data),
];
}
/**
* Returns the hash of the user-rendered toolbar subtrees and cacheability.
*
* @return array
* An array with the hash of the toolbar subtrees and cacheability.
*/
function _toolbar_get_subtrees_hash() {
[$subtrees, $cacheability] = toolbar_get_rendered_subtrees();
$hash = Crypt::hashBase64(serialize($subtrees));
return [
$hash,
$cacheability,
];
}
Functions
Title | Deprecated | Summary |
---|---|---|
template_preprocess_toolbar | in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). |
Prepares variables for administration toolbar templates. |
toolbar_get_rendered_subtrees | Returns the rendered subtree of each top-level toolbar link. | |
toolbar_menu_navigation_links | Adds toolbar-specific attributes to the menu link tree. | |
_toolbar_get_subtrees_hash | Returns the hash of the user-rendered toolbar subtrees and cacheability. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.