3

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?

2 Answers 2

6

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
  • This is the correct answer. Commented May 24, 2019 at 14:58
  • This did not work for me, Drupal 10. Also a couple of typos. Commented Aug 18, 2023 at 14:45
7

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);
  }
}
1
  • Yes this worked for me Drupal 10. Commented Aug 18, 2023 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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