ViewsLocalTask.php in Drupal 9
File
core/modules/views/src/Plugin/Derivative/ViewsLocalTask.php
View source
<?php
namespace Drupal\views\Plugin\Derivative;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ViewsLocalTask extends DeriverBase implements ContainerDeriverInterface {
protected $routeProvider;
protected $state;
protected $viewStorage;
public function __construct(RouteProviderInterface $route_provider, StateInterface $state, EntityStorageInterface $view_storage) {
$this->routeProvider = $route_provider;
$this->state = $state;
$this->viewStorage = $view_storage;
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($container
->get('router.route_provider'), $container
->get('state'), $container
->get('entity_type.manager')
->getStorage('view'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
$this->derivatives = [];
$view_route_names = $this->state
->get('views.view_route_names');
foreach ($this
->getApplicableMenuViews() as $pair) {
list($view_id, $display_id) = $pair;
$executable = $this->viewStorage
->load($view_id)
->getExecutable();
$executable
->setDisplay($display_id);
$menu = $executable->display_handler
->getOption('menu');
if (in_array($menu['type'], [
'tab',
'default tab',
])) {
$plugin_id = 'view.' . $executable->storage
->id() . '.' . $display_id;
$route_name = $view_route_names[$executable->storage
->id() . '.' . $display_id];
if ($route_name != $plugin_id) {
continue;
}
$this->derivatives[$plugin_id] = [
'route_name' => $route_name,
'weight' => $menu['weight'],
'title' => $menu['title'],
] + $base_plugin_definition;
if ($menu['type'] == 'default tab') {
$this->derivatives[$plugin_id]['base_route'] = $route_name;
}
}
}
return $this->derivatives;
}
public function alterLocalTasks(&$local_tasks) {
$view_route_names = $this->state
->get('views.view_route_names');
foreach ($this
->getApplicableMenuViews() as $pair) {
list($view_id, $display_id) = $pair;
$executable = $this->viewStorage
->load($view_id)
->getExecutable();
$executable
->setDisplay($display_id);
$menu = $executable->display_handler
->getOption('menu');
if (in_array($menu['type'], [
'tab',
])) {
$plugin_id = 'view.' . $executable->storage
->id() . '.' . $display_id;
$view_route_name = $view_route_names[$executable->storage
->id() . '.' . $display_id];
if ($view_route_name != $plugin_id) {
unset($local_tasks[$plugin_id]);
continue;
}
$path = $executable->display_handler
->getPath();
$split = explode('/', $path);
array_pop($split);
$path = implode('/', $split);
$pattern = '/' . str_replace('%', '{}', $path);
if ($routes = $this->routeProvider
->getRoutesByPattern($pattern)) {
foreach ($routes
->all() as $name => $route) {
$local_tasks['views_view:' . $plugin_id]['base_route'] = $name;
break;
}
}
}
}
}
protected function getApplicableMenuViews() {
return Views::getApplicableViews('uses_menu_links');
}
}
Classes
Name |
Description |
ViewsLocalTask |
Provides local task definitions for all views configured as local tasks. |