comparison core/modules/views/src/Plugin/Derivative/ViewsLocalTask.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\views\Plugin\Derivative;
4
5 use Drupal\Core\Entity\EntityStorageInterface;
6 use Drupal\Core\State\StateInterface;
7 use Drupal\Component\Plugin\Derivative\DeriverBase;
8 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
9 use Drupal\Core\Routing\RouteProviderInterface;
10 use Drupal\views\Views;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14 * Provides local task definitions for all views configured as local tasks.
15 */
16 class ViewsLocalTask extends DeriverBase implements ContainerDeriverInterface {
17
18 /**
19 * The route provider.
20 *
21 * @var \Drupal\Core\Routing\RouteProviderInterface
22 */
23 protected $routeProvider;
24
25 /**
26 * The state key value store.
27 *
28 * @var \Drupal\Core\State\StateInterface
29 */
30 protected $state;
31
32 /**
33 * The view storage.
34 *
35 * @var \Drupal\Core\Entity\EntityStorageInterface
36 */
37 protected $viewStorage;
38
39 /**
40 * Constructs a \Drupal\views\Plugin\Derivative\ViewsLocalTask instance.
41 *
42 * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
43 * The route provider.
44 * @param \Drupal\Core\State\StateInterface $state
45 * The state key value store.
46 * @param \Drupal\Core\Entity\EntityStorageInterface $view_storage
47 * The view storage.
48 */
49 public function __construct(RouteProviderInterface $route_provider, StateInterface $state, EntityStorageInterface $view_storage) {
50 $this->routeProvider = $route_provider;
51 $this->state = $state;
52 $this->viewStorage = $view_storage;
53 }
54
55 /**
56 * {@inheritdoc}
57 */
58 public static function create(ContainerInterface $container, $base_plugin_id) {
59 return new static(
60 $container->get('router.route_provider'),
61 $container->get('state'),
62 $container->get('entity.manager')->getStorage('view')
63 );
64 }
65
66 /**
67 * {@inheritdoc}
68 */
69 public function getDerivativeDefinitions($base_plugin_definition) {
70 $this->derivatives = [];
71
72 $view_route_names = $this->state->get('views.view_route_names');
73 foreach ($this->getApplicableMenuViews() as $pair) {
74 /** @var $executable \Drupal\views\ViewExecutable */
75 list($view_id, $display_id) = $pair;
76 $executable = $this->viewStorage->load($view_id)->getExecutable();
77
78 $executable->setDisplay($display_id);
79 $menu = $executable->display_handler->getOption('menu');
80 if (in_array($menu['type'], ['tab', 'default tab'])) {
81 $plugin_id = 'view.' . $executable->storage->id() . '.' . $display_id;
82 $route_name = $view_route_names[$executable->storage->id() . '.' . $display_id];
83
84 // Don't add a local task for views which override existing routes.
85 // @todo Alternative it could just change the existing entry.
86 if ($route_name != $plugin_id) {
87 continue;
88 }
89
90 $this->derivatives[$plugin_id] = [
91 'route_name' => $route_name,
92 'weight' => $menu['weight'],
93 'title' => $menu['title'],
94 ] + $base_plugin_definition;
95
96 // Default local tasks have themselves as root tab.
97 if ($menu['type'] == 'default tab') {
98 $this->derivatives[$plugin_id]['base_route'] = $route_name;
99 }
100 }
101 }
102 return $this->derivatives;
103 }
104
105 /**
106 * Alters base_route and parent_id into the views local tasks.
107 */
108 public function alterLocalTasks(&$local_tasks) {
109 $view_route_names = $this->state->get('views.view_route_names');
110
111 foreach ($this->getApplicableMenuViews() as $pair) {
112 list($view_id, $display_id) = $pair;
113 /** @var $executable \Drupal\views\ViewExecutable */
114 $executable = $this->viewStorage->load($view_id)->getExecutable();
115
116 $executable->setDisplay($display_id);
117 $menu = $executable->display_handler->getOption('menu');
118
119 // We already have set the base_route for default tabs.
120 if (in_array($menu['type'], ['tab'])) {
121 $plugin_id = 'view.' . $executable->storage->id() . '.' . $display_id;
122 $view_route_name = $view_route_names[$executable->storage->id() . '.' . $display_id];
123
124 // Don't add a local task for views which override existing routes.
125 if ($view_route_name != $plugin_id) {
126 unset($local_tasks[$plugin_id]);
127 continue;
128 }
129
130 // Find out the parent route.
131 // @todo Find out how to find both the root and parent tab.
132 $path = $executable->display_handler->getPath();
133 $split = explode('/', $path);
134 array_pop($split);
135 $path = implode('/', $split);
136
137 $pattern = '/' . str_replace('%', '{}', $path);
138 if ($routes = $this->routeProvider->getRoutesByPattern($pattern)) {
139 foreach ($routes->all() as $name => $route) {
140 $local_tasks['views_view:' . $plugin_id]['base_route'] = $name;
141 // Skip after the first found route.
142 break;
143 }
144 }
145 }
146 }
147 }
148
149 /**
150 * Return a list of all views and display IDs that have a menu entry.
151 *
152 * @return array
153 * A list of arrays containing the $view and $display_id.
154 * @code
155 * array(
156 * array($view, $display_id),
157 * array($view, $display_id),
158 * );
159 * @endcode
160 */
161 protected function getApplicableMenuViews() {
162 return Views::getApplicableViews('uses_menu_links');
163 }
164
165 }