From cffeb24f20c274de0c28d43729da3729cc6fb30a Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 16 May 2022 14:35:23 +0100 Subject: [PATCH] Issue #2580263 by Berdir, nils.destoop, catch, Cottser, larowlan: Find a way to not run contextual_preprocess() on every template --- core/modules/contextual/contextual.module | 30 +++++++++++------------ 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/core/modules/contextual/contextual.module b/core/modules/contextual/contextual.module index 1e1038d3558..5e0b67826c2 100644 --- a/core/modules/contextual/contextual.module +++ b/core/modules/contextual/contextual.module @@ -108,11 +108,6 @@ function contextual_help($route_name, RouteMatchInterface $route_match) { * @see \Drupal\contextual\ContextualController::render() */ function contextual_preprocess(&$variables, $hook, $info) { - $variables['#cache']['contexts'][] = 'user.permissions'; - if (!\Drupal::currentUser()->hasPermission('access contextual links')) { - return; - } - // Determine the primary theme function argument. if (!empty($info['variables'])) { $keys = array_keys($info['variables']); @@ -126,18 +121,21 @@ function contextual_preprocess(&$variables, $hook, $info) { } if (isset($element) && is_array($element) && !empty($element['#contextual_links'])) { - // Mark this element as potentially having contextual links attached to it. - $variables['attributes']['class'][] = 'contextual-region'; + $variables['#cache']['contexts'][] = 'user.permissions'; + if (\Drupal::currentUser()->hasPermission('access contextual links')) { + // Mark this element as potentially having contextual links attached to it. + $variables['attributes']['class'][] = 'contextual-region'; - // Renders a contextual links placeholder unconditionally, thus not breaking - // the render cache. Although the empty placeholder is rendered for all - // users, contextual_page_attachments() only adds the asset library for - // users with the 'access contextual links' permission, thus preventing - // unnecessary HTTP requests for users without that permission. - $variables['title_suffix']['contextual_links'] = [ - '#type' => 'contextual_links_placeholder', - '#id' => _contextual_links_to_id($element['#contextual_links']), - ]; + // Renders a contextual links placeholder unconditionally, thus not breaking + // the render cache. Although the empty placeholder is rendered for all + // users, contextual_page_attachments() only adds the asset library for + // users with the 'access contextual links' permission, thus preventing + // unnecessary HTTP requests for users without that permission. + $variables['title_suffix']['contextual_links'] = [ + '#type' => 'contextual_links_placeholder', + '#id' => _contextual_links_to_id($element['#contextual_links']), + ]; + } } }