function 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.
1 call to template_preprocess_block()
BlockTemplateSuggestionsUnitTest::testBlockThemeHookSuggestions in drupal/core/modules/block/lib/Drupal/block/Tests/BlockTemplateSuggestionsUnitTest.php
Test if template_preprocess_block() handles the suggestions right.

File

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

Code

function template_preprocess_block(&$variables) {
  $block_counter =& drupal_static(__FUNCTION__, array());
  $variables['configuration'] = $variables['elements']['#configuration'];
  $variables['plugin_id'] = $variables['elements']['#plugin_id'];
  $variables['label'] = !empty($variables['configuration']['label_display']) ? $variables['configuration']['label'] : '';
  $variables['content'] = $variables['elements']['content'];
  $variables['attributes']['class'][] = 'block';
  $variables['attributes']['class'][] = drupal_html_class('block-' . $variables['configuration']['module']);

  // The block template provides a wrapping element for the content. Render the
  // #attributes of the content on this wrapping element rather than passing
  // them through to the content's #theme function/template. This allows the
  // content to not require a function/template at all, or if it does use one,
  // to not require it to output an extra wrapping element.
  if (isset($variables['content']['#attributes'])) {
    $variables['content_attributes'] = NestedArray::mergeDeep($variables['content_attributes'], $variables['content']['#attributes']);
    unset($variables['content']['#attributes']);
  }

  // Add default class for block content.
  $variables['content_attributes']['class'][] = 'content';
  $variables['theme_hook_suggestions'][] = 'block__' . $variables['configuration']['module'];

  // Hyphens (-) and underscores (_) play a special role in theme suggestions.
  // Theme suggestions should only contain underscores, because within
  // drupal_find_theme_templates(), underscores are converted to hyphens to
  // match template file names, and then converted back to underscores to match
  // pre-processing and other function names. So if your theme suggestion
  // contains a hyphen, it will end up as an underscore after this conversion,
  // and your function names won't be recognized. So, we need to convert
  // hyphens to underscores in block deltas for the theme suggestions.
  // We can safely explode on : because we know the Block plugin type manager
  // enforces that delimiter for all derivatives.
  $parts = explode(':', $variables['plugin_id']);
  $suggestion = 'block';
  while ($part = array_shift($parts)) {
    $variables['theme_hook_suggestions'][] = $suggestion .= '__' . strtr($part, '-', '_');
  }

  // Create a valid HTML ID and make sure it is unique.
  if ($id = $variables['elements']['#block']
    ->id()) {
    $config_id = explode('.', $id);
    $machine_name = array_pop($config_id);
    $variables['attributes']['id'] = drupal_html_id('block-' . $machine_name);
    $variables['theme_hook_suggestions'][] = 'block__' . $machine_name;
  }
}