Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / devel / src / ToolbarHandler.php
1 <?php
2
3 namespace Drupal\devel;
4
5 use Drupal\Core\Cache\CacheableMetadata;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
8 use Drupal\Core\Menu\MenuLinkTreeInterface;
9 use Drupal\Core\Menu\MenuTreeParameters;
10 use Drupal\Core\Session\AccountProxyInterface;
11 use Drupal\Core\StringTranslation\StringTranslationTrait;
12 use Drupal\Core\Url;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Toolbar integration handler.
17  */
18 class ToolbarHandler implements ContainerInjectionInterface {
19
20   use StringTranslationTrait;
21
22   /**
23    * The menu link tree service.
24    *
25    * @var \Drupal\Core\Menu\MenuLinkTreeInterface
26    */
27   protected $menuLinkTree;
28
29   /**
30    * The devel toolbar config.
31    *
32    * @var \Drupal\Core\Config\ImmutableConfig
33    */
34   protected $config;
35
36   /**
37    * The current user.
38    *
39    * @var \Drupal\Core\Session\AccountProxyInterface
40    */
41   protected $account;
42
43   /**
44    * ToolbarHandler constructor.
45    *
46    * @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_link_tree
47    *   The menu link tree service.
48    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
49    *   The config factory.
50    * @param \Drupal\Core\Session\AccountProxyInterface $account
51    *   The current user.
52    */
53   public function __construct(MenuLinkTreeInterface $menu_link_tree, ConfigFactoryInterface $config_factory, AccountProxyInterface $account) {
54     $this->menuLinkTree = $menu_link_tree;
55     $this->config = $config_factory->get('devel.toolbar.settings');
56     $this->account = $account;
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public static function create(ContainerInterface $container) {
63     return new static(
64       $container->get('toolbar.menu_tree'),
65       $container->get('config.factory'),
66       $container->get('current_user')
67     );
68   }
69
70   /**
71    * Hook bridge.
72    *
73    * @return array
74    *   The devel toolbar items render array.
75    *
76    * @see hook_toolbar()
77    */
78   public function toolbar() {
79     $items['devel'] = [
80       '#cache' => [
81         'contexts' => ['user.permissions'],
82       ],
83     ];
84
85     if ($this->account->hasPermission('access devel information')) {
86       $items['devel'] += [
87         '#type' => 'toolbar_item',
88         '#weight' => 999,
89         'tab' => [
90           '#type' => 'link',
91           '#title' => $this->t('Devel'),
92           '#url' => Url::fromRoute('devel.admin_settings'),
93           '#attributes' => [
94             'title' => $this->t('Development menu'),
95             'class' => ['toolbar-icon', 'toolbar-icon-devel'],
96           ],
97         ],
98         'tray' => [
99           '#heading' => $this->t('Development menu'),
100           'devel_menu' => [
101             // Currently devel menu is uncacheable, so instead of poisoning the
102             // entire page cache we use a lazy builder.
103             // @see \Drupal\devel\Plugin\Menu\DestinationMenuLink
104             // @see \Drupal\devel\Plugin\Menu\RouteDetailMenuItem
105             '#lazy_builder' => [ToolbarHandler::class . ':lazyBuilder', []],
106             // Force the creation of the placeholder instead of rely on the
107             // automatical placeholdering or otherwise the page results
108             // uncacheable when max-age 0 is bubbled up.
109             '#create_placeholder' => TRUE,
110           ],
111           'configuration' => [
112             '#type' => 'link',
113             '#title' => $this->t('Configure'),
114             '#url' => Url::fromRoute('devel.toolbar.settings_form'),
115             '#options' => [
116               'attributes' => ['class' => ['edit-devel-toolbar']],
117             ],
118           ],
119         ],
120         '#attached' => [
121           'library' => 'devel/devel-toolbar',
122         ],
123       ];
124     }
125
126     return $items;
127   }
128
129   /**
130    * Lazy builder callback for the devel menu toolbar.
131    *
132    * @return array
133    *   The renderable array rapresentation of the devel menu.
134    */
135   public function lazyBuilder() {
136     $parameters = new MenuTreeParameters();
137     $parameters->onlyEnabledLinks()->setTopLevelOnly();
138
139     $tree = $this->menuLinkTree->load('devel', $parameters);
140
141     $manipulators = [
142       ['callable' => 'menu.default_tree_manipulators:checkAccess'],
143       ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
144       ['callable' => ToolbarHandler::class . ':processTree'],
145     ];
146     $tree = $this->menuLinkTree->transform($tree, $manipulators);
147
148     $build = $this->menuLinkTree->build($tree);
149
150     CacheableMetadata::createFromRenderArray($build)
151       ->addCacheableDependency($this->config)
152       ->applyTo($build);
153
154     return $build;
155   }
156
157   /**
158    * Adds toolbar-specific attributes to the menu link tree.
159    *
160    * @param \Drupal\Core\Menu\MenuLinkTreeElement[] $tree
161    *   The menu link tree to manipulate.
162    *
163    * @return \Drupal\Core\Menu\MenuLinkTreeElement[]
164    *   The manipulated menu link tree.
165    */
166   public function processTree(array $tree) {
167     $visible_items = $this->config->get('toolbar_items') ?: [];
168
169     foreach ($tree as $element) {
170       $plugin_id = $element->link->getPluginId();
171       if (!in_array($plugin_id, $visible_items)) {
172         // Add a class that allow to hide the non prioritized menu items when
173         // the toolbar has horizontal orientation.
174         $element->options['attributes']['class'][] = 'toolbar-horizontal-item-hidden';
175       }
176     }
177
178     return $tree;
179   }
180
181 }