LocalTasksBlock.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. $tabs = [
  80. '#theme' => 'menu_local_tasks',
  81. ];
  82. // Add only selected levels for the printed output.
  83. if ($config['primary']) {
  84. $links = $this->localTaskManager->getLocalTasks($this->routeMatch->getRouteName(), 0);
  85. $cacheability = $cacheability->merge($links['cacheability']);
  86. // Do not display single tabs.
  87. $tabs += [
  88. '#primary' => count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : [],
  89. ];
  90. }
  91. if ($config['secondary']) {
  92. $links = $this->localTaskManager->getLocalTasks($this->routeMatch->getRouteName(), 1);
  93. $cacheability = $cacheability->merge($links['cacheability']);
  94. // Do not display single tabs.
  95. $tabs += [
  96. '#secondary' => count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : [],
  97. ];
  98. }
  99. $build = [];
  100. $cacheability->applyTo($build);
  101. if (empty($tabs['#primary']) && empty($tabs['#secondary'])) {
  102. return $build;
  103. }
  104. return $build + $tabs;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function blockForm($form, FormStateInterface $form_state) {
  110. $config = $this->configuration;
  111. $defaults = $this->defaultConfiguration();
  112. $form['levels'] = [
  113. '#type' => 'details',
  114. '#title' => $this->t('Shown tabs'),
  115. '#description' => $this->t('Select tabs being shown in the block'),
  116. // Open if not set to defaults.
  117. '#open' => $defaults['primary'] !== $config['primary'] || $defaults['secondary'] !== $config['secondary'],
  118. ];
  119. $form['levels']['primary'] = [
  120. '#type' => 'checkbox',
  121. '#title' => $this->t('Show primary tabs'),
  122. '#default_value' => $config['primary'],
  123. ];
  124. $form['levels']['secondary'] = [
  125. '#type' => 'checkbox',
  126. '#title' => $this->t('Show secondary tabs'),
  127. '#default_value' => $config['secondary'],
  128. ];
  129. return $form;
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function blockSubmit($form, FormStateInterface $form_state) {
  135. $levels = $form_state->getValue('levels');
  136. $this->configuration['primary'] = $levels['primary'];
  137. $this->configuration['secondary'] = $levels['secondary'];
  138. }
  139. }