function template_preprocess_block

Same name and namespace in other branches
  1. 7.x modules/block/block.module \template_preprocess_block()
  2. 8.9.x core/modules/block/block.module \template_preprocess_block()
  3. 10 core/modules/block/block.module \template_preprocess_block()
  4. 11.x core/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

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['in_preview'] = $variables['elements']['#in_preview'] ?? FALSE;
    $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'];
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.