View source
<?php
namespace Drupal\devel;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ToolbarHandler implements ContainerInjectionInterface, TrustedCallbackInterface {
use StringTranslationTrait;
protected $menuLinkTree;
protected $config;
protected $account;
public function __construct(MenuLinkTreeInterface $menu_link_tree, ConfigFactoryInterface $config_factory, AccountProxyInterface $account) {
$this->menuLinkTree = $menu_link_tree;
$this->config = $config_factory
->get('devel.toolbar.settings');
$this->account = $account;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('toolbar.menu_tree'), $container
->get('config.factory'), $container
->get('current_user'));
}
public static function trustedCallbacks() {
return [
'lazyBuilder',
];
}
public function toolbar() {
$items['devel'] = [
'#cache' => [
'contexts' => [
'user.permissions',
],
],
];
if ($this->account
->hasPermission('access devel information')) {
$items['devel'] += [
'#type' => 'toolbar_item',
'#weight' => 999,
'tab' => [
'#type' => 'link',
'#title' => $this
->t('Devel'),
'#url' => Url::fromRoute('devel.admin_settings'),
'#attributes' => [
'title' => $this
->t('Development menu'),
'class' => [
'toolbar-icon',
'toolbar-icon-devel',
],
],
],
'tray' => [
'#heading' => $this
->t('Development menu'),
'devel_menu' => [
'#lazy_builder' => [
ToolbarHandler::class . ':lazyBuilder',
[],
],
'#create_placeholder' => TRUE,
],
'configuration' => [
'#type' => 'link',
'#title' => $this
->t('Configure'),
'#url' => Url::fromRoute('devel.toolbar.settings_form'),
'#options' => [
'attributes' => [
'class' => [
'edit-devel-toolbar',
],
],
],
],
],
'#attached' => [
'library' => 'devel/devel-toolbar',
],
];
}
return $items;
}
public function lazyBuilder() {
$parameters = new MenuTreeParameters();
$parameters
->onlyEnabledLinks()
->setTopLevelOnly();
$tree = $this->menuLinkTree
->load('devel', $parameters);
$manipulators = [
[
'callable' => 'menu.default_tree_manipulators:checkAccess',
],
[
'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
],
[
'callable' => ToolbarHandler::class . ':processTree',
],
];
$tree = $this->menuLinkTree
->transform($tree, $manipulators);
$build = $this->menuLinkTree
->build($tree);
CacheableMetadata::createFromRenderArray($build)
->addCacheableDependency($this->config)
->applyTo($build);
return $build;
}
public function processTree(array $tree) {
$visible_items = $this->config
->get('toolbar_items') ?: [];
foreach ($tree as $element) {
$plugin_id = $element->link
->getPluginId();
if (!in_array($plugin_id, $visible_items)) {
$element->options['attributes']['class'][] = 'toolbar-horizontal-item-hidden';
}
}
return $tree;
}
}