LocalTasksBlock.php 4.6 KB

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