Hi... I need a help with Drupal 8 templates, please.

I created a custom block, I can create a theme for the custom block (the instance), but I can't create a theme for the Custom Block TYPE (let's call it "the prototype").

I meant, is there a way to theming the different Custom Block Types?

And a "bonus question": Why in the Theme Debug I can see "block--block-content.html.twig" twice? Should I see one "block--block-content.html.twig" and one "block--CUSTOM-BLOCK-TYPE-content.html.twig"

I'm running Drupal 8.1 stable.

Comments

Jeff Burnz’s picture

By default there is no way to theme custom content block bundles (type), however for Adaptivetheme I developed a way that you can, and you can implement this in your theme.

This would give you the block template: block--bundle--the-prototype.html.twig

/**
 * Implements hook_theme_suggestions_HOOK_alter() for form templates.
 * @param array $suggestions
 * @param array $variables
 */
function THEMENAME_theme_suggestions_block_alter(array &$suggestions, array $variables) {
  // Block suggestions for custom block bundles.
  if (isset($variables['elements']['content']['#block_content'])) {
    array_splice($suggestions, 1, 0, 'block__bundle__' . $variables['elements']['content']['#block_content']->bundle());
  }
}

I can't answer the bonus question, suppose it's a bug. Notice I use array_splice() to slot the template suggestion in the array rather than pushing it on the end and overriding the template for the instance.

jrugel’s picture

It worked for me. Thanks a lot.

germaike mv’s picture

This works for me as well. nice work!

sprite’s picture

Following for notes purposes.

spritefully yours
Technical assistance provided to the Drupal community on my own time ...
Thank yous appreciated ...

Ismail Cherri’s picture

Nice!! Worked for me, thanks!

thomas.frobieter’s picture

Worked for me too. Whyever this isn't already in core o.O

aburrows’s picture

mmjvb’s picture

.

mmjvb’s picture

- test on BlockContentInterface
- adding suggestion at the end instead of inserting

deuriy’s picture

Thank you very much! It worked for me great!

MrShawnTucker’s picture

Worked great, thank you.