3 namespace Drupal\Core\Menu\Plugin\Block;
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;
15 * Provides a "Tabs" block to display the local tasks.
18 * id = "local_tasks_block",
19 * admin_label = @Translation("Tabs"),
22 class LocalTasksBlock extends BlockBase implements ContainerFactoryPluginInterface {
25 * The local task manager.
27 * @var \Drupal\Core\Menu\LocalTaskManagerInterface
29 protected $localTaskManager;
34 * @var \Drupal\Core\Routing\RouteMatchInterface
36 protected $routeMatch;
39 * Creates a LocalTasksBlock instance.
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
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;
61 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
66 $container->get('plugin.manager.menu.local_task'),
67 $container->get('current_route_match')
74 public function defaultConfiguration() {
76 'label_display' => FALSE,
85 public function build() {
86 $config = $this->configuration;
87 $cacheability = new CacheableMetadata();
88 $cacheability->addCacheableDependency($this->localTaskManager);
90 '#theme' => 'menu_local_tasks',
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.
99 '#primary' => count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : [],
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.
107 '#secondary' => count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : [],
112 $cacheability->applyTo($build);
113 if (empty($tabs['#primary']) && empty($tabs['#secondary'])) {
117 return $build + $tabs;
123 public function blockForm($form, FormStateInterface $form_state) {
124 $config = $this->configuration;
125 $defaults = $this->defaultConfiguration();
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'],
134 $form['levels']['primary'] = [
135 '#type' => 'checkbox',
136 '#title' => $this->t('Show primary tabs'),
137 '#default_value' => $config['primary'],
139 $form['levels']['secondary'] = [
140 '#type' => 'checkbox',
141 '#title' => $this->t('Show secondary tabs'),
142 '#default_value' => $config['secondary'],
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'];