function BlockContentHooks::themeSuggestionsBlockAlter

Implements hook_theme_suggestions_HOOK_alter() for block templates.

File

core/modules/block_content/src/Hook/BlockContentHooks.php, line 115

Class

BlockContentHooks
Hook implementations for block_content.

Namespace

Drupal\block_content\Hook

Code

public function themeSuggestionsBlockAlter(array &$suggestions, array $variables) : void {
    $suggestions_new = [];
    $content = $variables['elements']['content'];
    $block_content = $variables['elements']['content']['#block_content'] ?? NULL;
    if ($block_content instanceof BlockContentInterface) {
        $bundle = $content['#block_content']->bundle();
        $view_mode = strtr($variables['elements']['#configuration']['view_mode'], '.', '_');
        $suggestions_new[] = 'block__block_content__view__' . $view_mode;
        $suggestions_new[] = 'block__block_content__type__' . $bundle;
        $suggestions_new[] = 'block__block_content__view_type__' . $bundle . '__' . $view_mode;
        if (!empty($variables['elements']['#id'])) {
            $suggestions_new[] = 'block__block_content__id__' . $variables['elements']['#id'];
            $suggestions_new[] = 'block__block_content__id_view__' . $variables['elements']['#id'] . '__' . $view_mode;
        }
        // Remove duplicate block__block_content.
        $suggestions = array_unique($suggestions);
        array_splice($suggestions, 1, 0, $suggestions_new);
    }
}

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