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(); $cacheability->addCacheableDependency($this->localTaskManager); $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']; } }