You are here

function contextual_preprocess in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/contextual/contextual.module \contextual_preprocess()
  2. 7 modules/contextual/contextual.module \contextual_preprocess()

Implements hook_preprocess().

See also

contextual_pre_render_placeholder()

contextual_page_attachments()

\Drupal\contextual\ContextualController::render()

File

core/modules/contextual/contextual.module, line 110
Adds contextual links to perform actions related to elements on a page.

Code

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']);
    $key = $keys[0];
  }
  elseif (!empty($info['render element'])) {
    $key = $info['render element'];
  }
  if (!empty($key) && isset($variables[$key])) {
    $element = $variables[$key];
  }
  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']),
    ];
  }
}