1

I have to create theme suggestions for my content block types (custom blocks - configured via admin/structure/block/block-content/types), because i didn't find them as default suggestions.

The default was:

FILE NAME SUGGESTIONS:
* block--registernowheaderblock.html.twig
* block--block-content--7bd0dc95-f2ee-4e70-8a5a-90d718384712.html.twig
x block--block-content.html.twig
x block--block-content.html.twig
* block.html.twig

block--block-content.html.twig is not enough, because i need different custom block type templates. (Btw. 2 times block--block-content.html.twig?)

I need:

FILE NAME SUGGESTIONS:
x block--block-content--header-content-block.html.twig
* ...
* block.html.twig

So i tried in the theme file:

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */

function MYTHEME_theme_suggestions_block_alter(array &$suggestions, array $variables) 
{
    $block = $variables['elements'];
    $blockType = $block['#configuration']['provider'];
    if ($blockType == "block_content") {
        $bundle = $block['content']['#block_content']->bundle();
        $suggestions[] = 'block__' . $blockType . '__' . $bundle;
    }
}

This brings up:

FILE NAME SUGGESTIONS:
x block--block-content--header-content-block.html.twig
* block--registernowheaderblock.html.twig
* block--block-content--7bd0dc95-f2ee-4e70-8a5a-90d718384712.html.twig
* block--block-content.html.twig
* block--block-content.html.twig
* block.html.twig

Nice - but my suggestion is higher then the id based ones for single blocks - not good ... so i added

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function oeb_theme_suggestions_block_alter(array &$suggestions, array $variables) 
{
    $block = $variables['elements'];
    $blockType = $block['#configuration']['provider'];
    if ($blockType == "block_content") {
        $bundle = $block['content']['#block_content']->bundle();
        $blockUuid = $block['content']['#block_content']->uuid();
        $blockId = $block['#id'];
        $suggestions[] = 'block__' . $blockType . '__' . $bundle;
        $suggestions[] = 'block__' . $blockType . '__' . $bundle . '__' . $blockId;
        $suggestions[] = 'block__' . $blockType . '__' . $bundle . '__' . $blockUuid;
    }
}

Result

FILE NAME SUGGESTIONS:
* block--block-content--header-content-block--7bd0dc95-f2ee-4e70-8a5a-90d718384712.html.twig
* block--block-content--header-content-block--registernowheaderblock.html.twig
x block--block-content--header-content-block.html.twig
* block--registernowheaderblock.html.twig
* block--block-content--7bd0dc95-f2ee-4e70-8a5a-90d718384712.html.twig
* block--block-content.html.twig
* block--block-content.html.twig
* block.html.twig

This is not nice, because of the doubled id stuff - is it possible to get something like:

FILE NAME SUGGESTIONS:
* block--registernowheaderblock.html.twig
* block--block-content--7bd0dc95-f2ee-4e70-8a5a-90d718384712.html.twig
x block--block-content--header-content-block.html.twig
* block--block-content.html.twig
* block.html.twig

Means: Single blocks are the highest - but unique? Or should i live with this to keep the stuff simple?

4

What you need is simple array manipulation. Basically split of the original suggestions add the the suggestions and merge the suggestions again.

Some like this should work (untested).

function oeb_theme_suggestions_block_alter(array &$suggestions, array $variables) {
  ...
  $first_sugestions = array_slice($suggestions, 0, 2); 
  $last_sugestions = array_slice($suggestions, 0, 2);
  $first_sugestions[] = 'block__' . $blockType . '__' . $bundle;
  $suggestion = array_merge($first_suggestions, $last_suggestions);
}
2

I add this hook in MYTHEME.theme:

function MYTHEME_theme_suggestions_block_alter(array &$suggestions, array $variables) {
  $block = $variables['elements'];
  $blockType = $block['#configuration']['provider'];
  if ($blockType == "block_content") {
    $bundle = $block['content']['#block_content']->bundle();
    array_splice($suggestions, 2, 0, 'block__' . $bundle);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.