diff --git a/core/modules/views/src/Element/View.php b/core/modules/views/src/Element/View.php index 9c11d3b..682de0b 100644 --- a/core/modules/views/src/Element/View.php +++ b/core/modules/views/src/Element/View.php @@ -70,7 +70,10 @@ public static function preRenderViewElement($element) { $element['#title'] = &$element['view_build']['#title']; } - views_add_contextual_links($element, 'view', $view, $view->current_display); + $element['#view_id'] = $view->storage->id(); + $element['#view_display_show_admin_links'] = $view->getShowAdminLinks(); + $element['#view_display_plugin_id'] = $view->display_handler->getPluginId(); + views_add_contextual_links($element, 'view', $view->current_display); } } diff --git a/core/modules/views/src/Plugin/Block/ViewsBlockBase.php b/core/modules/views/src/Plugin/Block/ViewsBlockBase.php index b87711f..56d0158 100644 --- a/core/modules/views/src/Plugin/Block/ViewsBlockBase.php +++ b/core/modules/views/src/Plugin/Block/ViewsBlockBase.php @@ -202,7 +202,10 @@ protected function addContextualLinks(&$output, $block_type = 'block') { $output = array('#markup' => $output); } // Add the contextual links. - views_add_contextual_links($output, $block_type, $this->view, $this->displayID); + $output['#view_id'] = $this->view->storage->id(); + $output['#view_display_show_admin_links'] = $this->view->getShowAdminLinks(); + $output['#view_display_plugin_id'] = $this->view->display_handler->getPluginId(); + views_add_contextual_links($output, $block_type, $this->displayID); } } diff --git a/core/modules/views/src/Plugin/views/display/PathPluginBase.php b/core/modules/views/src/Plugin/views/display/PathPluginBase.php index e03a998..f69c8df 100644 --- a/core/modules/views/src/Plugin/views/display/PathPluginBase.php +++ b/core/modules/views/src/Plugin/views/display/PathPluginBase.php @@ -138,6 +138,7 @@ protected function getRoute($view_id, $display_id) { '_controller' => 'Drupal\views\Routing\ViewPageController::handle', 'view_id' => $view_id, 'display_id' => $display_id, + '_view_display_show_admin_links' => $this->getOption('show_admin_links'); ); // @todo How do we apply argument validation? diff --git a/core/modules/views/views.module b/core/modules/views/views.module index eea4603..2d96a9a 100644 --- a/core/modules/views/views.module +++ b/core/modules/views/views.module @@ -309,9 +309,10 @@ function views_page_display_pre_render(array $element) { // next to the page title. $route = \Drupal::routeMatch()->getRouteObject(); if ($route && $route->hasDefault('view_id')) { - $view = Views::getView($route->getDefault('view_id')); - $view->setDisplay($route->getDefault('display_id')); - views_add_contextual_links($element, 'page', $view, $view->current_display); + $element['#view_id'] = $route->getDefault('view_id'); + $element['#view_display_show_admin_links'] = $route->getOption('_view_display_show_admin_links'); + $element['#view_display_plugin_id'] = $route->getDefault('display_id'); + views_add_contextual_links($element, 'page', $route->getDefault('display_id')); } return $element; } @@ -404,15 +405,20 @@ function views_preprocess_html(&$variables) { * @see views_preprocess_page() * @see template_preprocess_views_view() */ -function views_add_contextual_links(&$render_element, $location, ViewExecutable $view, $display_id) { +function views_add_contextual_links(&$render_element, $location, $display_id) { + + $render_element['#cache_properties'] = ['view_id', 'view_display_show_admin_links', 'view_display_plugin_id']; + $view_id = $render_element['#view_id']; + $show_admin_links = $render_element['#view_display_show_admin_links']; + $display_plugin_id = $render_element['#view_display_plugin_id']; + // Do not do anything if the view is configured to hide its administrative // links or if the Contextual Links module is not enabled. - if (\Drupal::moduleHandler()->moduleExists('contextual') && $view->getShowAdminLinks()) { + if (\Drupal::moduleHandler()->moduleExists('contextual') && $show_admin_links) { // Also do not do anything if the display plugin has not defined any // contextual links that are intended to be displayed in the requested // location. - $plugin_id = $view->displayHandlers->get($display_id)->getPluginId(); - $plugin = Views::pluginManager('display')->getDefinition($plugin_id); + $plugin = Views::pluginManager('display')->getDefinition($display_plugin_id); // If contextual_links_locations are not set, provide a sane default. (To // avoid displaying any contextual links at all, a display plugin can still // set 'contextual_links_locations' to, e.g., {""}.) @@ -435,17 +441,20 @@ function views_add_contextual_links(&$render_element, $location, ViewExecutable $args = array(); $valid = TRUE; if (!empty($link['route_parameters_names'])) { + $view_storage = \Drupal::entityManager() + ->getStorage('view') + ->load($view_id); foreach ($link['route_parameters_names'] as $parameter_name => $property) { // If the plugin is trying to create an invalid contextual link // (for example, "path/to/{$view->storage->property}", where // $view->storage->{property} does not exist), we cannot construct // the link, so we skip it. - if (!property_exists($view->storage, $property)) { + if (!property_exists($view_storage, $property)) { $valid = FALSE; break; } else { - $args[$parameter_name] = $view->storage->get($property); + $args[$parameter_name] = $view_storage->get($property); } } } @@ -457,13 +466,14 @@ function views_add_contextual_links(&$render_element, $location, ViewExecutable 'route_parameters' => $args, 'metadata' => array( 'location' => $location, - 'name' => $view->storage->id(), + 'name' => $view_id, 'display_id' => $display_id, ), ); // If we're setting contextual links on a page, for a page view, for a // user that may use contextual links, attach Views' contextual links // JavaScript. + $render_element['#cache']['contexts'][] = 'user.permissions'; if ($location === 'page' && $render_element['#type'] === 'page' && \Drupal::currentUser()->hasPermission('access contextual links')) { $render_element['#attached']['library'][] = 'views/views.contextual-links'; }