Toolbar.php in Drupal 8
File
core/modules/toolbar/src/Element/Toolbar.php
View source
<?php
namespace Drupal\toolbar\Element;
use Drupal\Component\Utility\Html;
use Drupal\Core\Render\Element\RenderElement;
use Drupal\Core\Render\Element;
class Toolbar extends RenderElement {
public function getInfo() {
$class = get_class($this);
return [
'#pre_render' => [
[
$class,
'preRenderToolbar',
],
],
'#theme' => 'toolbar',
'#attached' => [
'library' => [
'toolbar/toolbar',
],
],
'#attributes' => [
'id' => 'toolbar-administration',
'role' => 'group',
'aria-label' => $this
->t('Site administration toolbar'),
],
'#bar' => [
'#heading' => $this
->t('Toolbar items'),
'#attributes' => [
'id' => 'toolbar-bar',
'role' => 'navigation',
'aria-label' => $this
->t('Toolbar items'),
],
],
];
}
public static function preRenderToolbar($element) {
$breakpoints = static::breakpointManager()
->getBreakpointsByGroup('toolbar');
if (!empty($breakpoints)) {
$media_queries = [];
foreach ($breakpoints as $id => $breakpoint) {
$media_queries[$id] = $breakpoint
->getMediaQuery();
}
$element['#attached']['drupalSettings']['toolbar']['breakpoints'] = $media_queries;
}
$module_handler = static::moduleHandler();
$items = $module_handler
->invokeAll('toolbar');
$module_handler
->alter('toolbar', $items);
uasort($items, [
'\\Drupal\\Component\\Utility\\SortArray',
'sortByWeightProperty',
]);
$element = array_merge($element, $items);
foreach (Element::children($element) as $key) {
$element[$key]['#id'] = Html::getId('toolbar-item-' . $key);
}
return $element;
}
protected static function breakpointManager() {
return \Drupal::service('breakpoint.manager');
}
protected static function moduleHandler() {
return \Drupal::moduleHandler();
}
}
Classes
Name |
Description |
Toolbar |
Provides a render element for the default Drupal toolbar. |