You are here

function template_preprocess_block in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/block/block.module \template_preprocess_block()
  2. 6 includes/theme.inc \template_preprocess_block()
  3. 7 modules/block/block.module \template_preprocess_block()

Prepares variables for block templates.

Default template: block.html.twig.

Prepares the values passed to the theme_block function to be passed into a pluggable template engine. Uses block properties to generate a series of template file suggestions. If none are found, the default block.html.twig is used.

Most themes use their own copy of block.html.twig. The default is located inside "core/modules/block/templates/block.html.twig". Look in there for the full list of available variables.

Parameters

array $variables: An associative array containing:

  • elements: An associative array containing the properties of the element. Properties used: #block, #configuration, #children, #plugin_id.

File

core/modules/block/block.module, line 244
Controls the visual building blocks a page is constructed with.

Code

function template_preprocess_block(&$variables) {
  $variables['configuration'] = $variables['elements']['#configuration'];
  $variables['plugin_id'] = $variables['elements']['#plugin_id'];
  $variables['base_plugin_id'] = $variables['elements']['#base_plugin_id'];
  $variables['derivative_plugin_id'] = $variables['elements']['#derivative_plugin_id'];
  $variables['label'] = !empty($variables['configuration']['label_display']) ? $variables['configuration']['label'] : '';
  $variables['content'] = $variables['elements']['content'];

  // A block's label is configuration: it is static. Allow dynamic labels to be
  // set in the render array.
  if (isset($variables['elements']['content']['#title']) && !empty($variables['configuration']['label_display'])) {
    $variables['label'] = $variables['elements']['content']['#title'];
  }

  // Create a valid HTML ID and make sure it is unique.
  if (!empty($variables['elements']['#id'])) {
    $variables['attributes']['id'] = Html::getUniqueId('block-' . $variables['elements']['#id']);
  }

  // Proactively add aria-describedby if possible to improve accessibility.
  if ($variables['label'] && isset($variables['attributes']['role'])) {
    $variables['title_attributes']['id'] = Html::getUniqueId($variables['label']);
    $variables['attributes']['aria-describedby'] = $variables['title_attributes']['id'];
  }
}