function block_theme_suggestions_block

Same name and namespace in other branches
  1. 9 core/modules/block/block.module \block_theme_suggestions_block()
  2. 8.9.x core/modules/block/block.module \block_theme_suggestions_block()
  3. 10 core/modules/block/block.module \block_theme_suggestions_block()

Implements hook_theme_suggestions_HOOK().

1 call to block_theme_suggestions_block()
BlockTemplateSuggestionsTest::testBlockThemeHookSuggestions in core/modules/block/tests/src/Kernel/BlockTemplateSuggestionsTest.php
Tests template suggestions from block_theme_suggestions_block().

File

core/modules/block/block.module, line 199

Code

function block_theme_suggestions_block(array $variables) {
    $suggestions = [];
    $suggestions[] = 'block__' . $variables['elements']['#configuration']['provider'];
    // 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['elements']['#plugin_id']);
    $suggestion = 'block';
    while ($part = array_shift($parts)) {
        $suggestions[] = $suggestion .= '__' . strtr($part, '-', '_');
    }
    if (!empty($variables['elements']['#id'])) {
        $suggestions[] = 'block__' . $variables['elements']['#id'];
    }
    return $suggestions;
}

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