Skip to content

Commit

Permalink
Issue #2580263 by Berdir, nils.destoop, catch, Cottser, larowlan: Fin…
Browse files Browse the repository at this point in the history
…d a way to not run contextual_preprocess() on every template
  • Loading branch information
alexpott committed May 16, 2022
1 parent cdaa87e commit 1693614
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions modules/contextual/contextual.module
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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';

// 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']),
];
$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']),
];
}
}
}

Expand Down

0 comments on commit 1693614

Please sign in to comment.