Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Menu / Plugin / Block / LocalTasksBlock.php
1 <?php
2
3 namespace Drupal\Core\Menu\Plugin\Block;
4
5 use Drupal\Core\Block\BlockBase;
6 use Drupal\Core\Cache\CacheableMetadata;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Menu\LocalTaskManagerInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\Core\Render\Element;
11 use Drupal\Core\Routing\RouteMatchInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Provides a "Tabs" block to display the local tasks.
16  *
17  * @Block(
18  *   id = "local_tasks_block",
19  *   admin_label = @Translation("Tabs"),
20  * )
21  */
22 class LocalTasksBlock extends BlockBase implements ContainerFactoryPluginInterface {
23
24   /**
25    * The local task manager.
26    *
27    * @var \Drupal\Core\Menu\LocalTaskManagerInterface
28    */
29   protected $localTaskManager;
30
31   /**
32    * The route match.
33    *
34    * @var \Drupal\Core\Routing\RouteMatchInterface
35    */
36   protected $routeMatch;
37
38   /**
39    * Creates a LocalTasksBlock instance.
40    *
41    * @param array $configuration
42    *   A configuration array containing information about the plugin instance.
43    * @param string $plugin_id
44    *   The plugin_id for the plugin instance.
45    * @param mixed $plugin_definition
46    *   The plugin implementation definition.
47    * @param \Drupal\Core\Menu\LocalTaskManagerInterface $local_task_manager
48    *   The local task manager.
49    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
50    *   The route match.
51    */
52   public function __construct(array $configuration, $plugin_id, $plugin_definition, LocalTaskManagerInterface $local_task_manager, RouteMatchInterface $route_match) {
53     parent::__construct($configuration, $plugin_id, $plugin_definition);
54     $this->localTaskManager = $local_task_manager;
55     $this->routeMatch = $route_match;
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
62     return new static(
63       $configuration,
64       $plugin_id,
65       $plugin_definition,
66       $container->get('plugin.manager.menu.local_task'),
67       $container->get('current_route_match')
68     );
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function defaultConfiguration() {
75     return [
76       'label_display' => FALSE,
77       'primary' => TRUE,
78       'secondary' => TRUE,
79     ];
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function build() {
86     $config = $this->configuration;
87     $cacheability = new CacheableMetadata();
88     $cacheability->addCacheableDependency($this->localTaskManager);
89     $tabs = [
90       '#theme' => 'menu_local_tasks',
91     ];
92
93     // Add only selected levels for the printed output.
94     if ($config['primary']) {
95       $links = $this->localTaskManager->getLocalTasks($this->routeMatch->getRouteName(), 0);
96       $cacheability = $cacheability->merge($links['cacheability']);
97       // Do not display single tabs.
98       $tabs += [
99         '#primary' => count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : [],
100       ];
101     }
102     if ($config['secondary']) {
103       $links = $this->localTaskManager->getLocalTasks($this->routeMatch->getRouteName(), 1);
104       $cacheability = $cacheability->merge($links['cacheability']);
105       // Do not display single tabs.
106       $tabs += [
107         '#secondary' => count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : [],
108       ];
109     }
110
111     $build = [];
112     $cacheability->applyTo($build);
113     if (empty($tabs['#primary']) && empty($tabs['#secondary'])) {
114       return $build;
115     }
116
117     return $build + $tabs;
118   }
119
120   /**
121    * {@inheritdoc}
122    */
123   public function blockForm($form, FormStateInterface $form_state) {
124     $config = $this->configuration;
125     $defaults = $this->defaultConfiguration();
126
127     $form['levels'] = [
128       '#type' => 'details',
129       '#title' => $this->t('Shown tabs'),
130       '#description' => $this->t('Select tabs being shown in the block'),
131       // Open if not set to defaults.
132       '#open' => $defaults['primary'] !== $config['primary'] || $defaults['secondary'] !== $config['secondary'],
133     ];
134     $form['levels']['primary'] = [
135       '#type' => 'checkbox',
136       '#title' => $this->t('Show primary tabs'),
137       '#default_value' => $config['primary'],
138     ];
139     $form['levels']['secondary'] = [
140       '#type' => 'checkbox',
141       '#title' => $this->t('Show secondary tabs'),
142       '#default_value' => $config['secondary'],
143     ];
144
145     return $form;
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   public function blockSubmit($form, FormStateInterface $form_state) {
152     $levels = $form_state->getValue('levels');
153     $this->configuration['primary'] = $levels['primary'];
154     $this->configuration['secondary'] = $levels['secondary'];
155   }
156
157 }