123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace Drupal\Core\Menu\Plugin\Block;
- use Drupal\Core\Block\BlockBase;
- use Drupal\Core\Cache\CacheableMetadata;
- use Drupal\Core\Form\FormStateInterface;
- use Drupal\Core\Menu\LocalTaskManagerInterface;
- use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
- use Drupal\Core\Render\Element;
- use Drupal\Core\Routing\RouteMatchInterface;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- /**
- * Provides a "Tabs" block to display the local tasks.
- *
- * @Block(
- * id = "local_tasks_block",
- * admin_label = @Translation("Tabs"),
- * )
- */
- class LocalTasksBlock extends BlockBase implements ContainerFactoryPluginInterface {
- /**
- * The local task manager.
- *
- * @var \Drupal\Core\Menu\LocalTaskManagerInterface
- */
- protected $localTaskManager;
- /**
- * The route match.
- *
- * @var \Drupal\Core\Routing\RouteMatchInterface
- */
- protected $routeMatch;
- /**
- * Creates a LocalTasksBlock instance.
- *
- * @param array $configuration
- * A configuration array containing information about the plugin instance.
- * @param string $plugin_id
- * The plugin_id for the plugin instance.
- * @param mixed $plugin_definition
- * The plugin implementation definition.
- * @param \Drupal\Core\Menu\LocalTaskManagerInterface $local_task_manager
- * The local task manager.
- * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
- * The route match.
- */
- public function __construct(array $configuration, $plugin_id, $plugin_definition, LocalTaskManagerInterface $local_task_manager, RouteMatchInterface $route_match) {
- parent::__construct($configuration, $plugin_id, $plugin_definition);
- $this->localTaskManager = $local_task_manager;
- $this->routeMatch = $route_match;
- }
- /**
- * {@inheritdoc}
- */
- public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
- return new static(
- $configuration,
- $plugin_id,
- $plugin_definition,
- $container->get('plugin.manager.menu.local_task'),
- $container->get('current_route_match')
- );
- }
- /**
- * {@inheritdoc}
- */
- public function defaultConfiguration() {
- return [
- 'label_display' => FALSE,
- 'primary' => TRUE,
- 'secondary' => TRUE,
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function build() {
- $config = $this->configuration;
- $cacheability = new CacheableMetadata();
- $tabs = [
- '#theme' => 'menu_local_tasks',
- ];
- // Add only selected levels for the printed output.
- if ($config['primary']) {
- $links = $this->localTaskManager->getLocalTasks($this->routeMatch->getRouteName(), 0);
- $cacheability = $cacheability->merge($links['cacheability']);
- // Do not display single tabs.
- $tabs += [
- '#primary' => count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : [],
- ];
- }
- if ($config['secondary']) {
- $links = $this->localTaskManager->getLocalTasks($this->routeMatch->getRouteName(), 1);
- $cacheability = $cacheability->merge($links['cacheability']);
- // Do not display single tabs.
- $tabs += [
- '#secondary' => count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : [],
- ];
- }
- $build = [];
- $cacheability->applyTo($build);
- if (empty($tabs['#primary']) && empty($tabs['#secondary'])) {
- return $build;
- }
- return $build + $tabs;
- }
- /**
- * {@inheritdoc}
- */
- public function blockForm($form, FormStateInterface $form_state) {
- $config = $this->configuration;
- $defaults = $this->defaultConfiguration();
- $form['levels'] = [
- '#type' => 'details',
- '#title' => $this->t('Shown tabs'),
- '#description' => $this->t('Select tabs being shown in the block'),
- // Open if not set to defaults.
- '#open' => $defaults['primary'] !== $config['primary'] || $defaults['secondary'] !== $config['secondary'],
- ];
- $form['levels']['primary'] = [
- '#type' => 'checkbox',
- '#title' => $this->t('Show primary tabs'),
- '#default_value' => $config['primary'],
- ];
- $form['levels']['secondary'] = [
- '#type' => 'checkbox',
- '#title' => $this->t('Show secondary tabs'),
- '#default_value' => $config['secondary'],
- ];
- return $form;
- }
- /**
- * {@inheritdoc}
- */
- public function blockSubmit($form, FormStateInterface $form_state) {
- $levels = $form_state->getValue('levels');
- $this->configuration['primary'] = $levels['primary'];
- $this->configuration['secondary'] = $levels['secondary'];
- }
- }
|