Issue #2580263 by Berdir, nils.destoop, catch, Cottser, larowlan: Find a way to not run contextual_preprocess() on every template

merge-requests/2291/head
Alex Pott 2022-05-16 14:35:23 +01:00
parent a1c4ca7b0c
commit cffeb24f20
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
1 changed files with 14 additions and 16 deletions

View File

@ -108,11 +108,6 @@ function contextual_help($route_name, RouteMatchInterface $route_match) {
* @see \Drupal\contextual\ContextualController::render() * @see \Drupal\contextual\ContextualController::render()
*/ */
function contextual_preprocess(&$variables, $hook, $info) { 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. // Determine the primary theme function argument.
if (!empty($info['variables'])) { if (!empty($info['variables'])) {
$keys = array_keys($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'])) { if (isset($element) && is_array($element) && !empty($element['#contextual_links'])) {
// Mark this element as potentially having contextual links attached to it. $variables['#cache']['contexts'][] = 'user.permissions';
$variables['attributes']['class'][] = 'contextual-region'; 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 // Renders a contextual links placeholder unconditionally, thus not breaking
// the render cache. Although the empty placeholder is rendered for all // the render cache. Although the empty placeholder is rendered for all
// users, contextual_page_attachments() only adds the asset library for // users, contextual_page_attachments() only adds the asset library for
// users with the 'access contextual links' permission, thus preventing // users with the 'access contextual links' permission, thus preventing
// unnecessary HTTP requests for users without that permission. // unnecessary HTTP requests for users without that permission.
$variables['title_suffix']['contextual_links'] = [ $variables['title_suffix']['contextual_links'] = [
'#type' => 'contextual_links_placeholder', '#type' => 'contextual_links_placeholder',
'#id' => _contextual_links_to_id($element['#contextual_links']), '#id' => _contextual_links_to_id($element['#contextual_links']),
]; ];
}
} }
} }