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!

DraganI’s picture

I have no idea how this works but it does :D

Can you explain to a newbie? 

Thanks :)

ndiesslin’s picture

This worked for me, thank you!

MrTucker’s picture

Worked great, thank you.

h.munster’s picture

Worked for me as well.
Thank you so much :)

CPxiom’s picture

Isn't this possible with https://www.drupal.org/project/components ? I recently learned about this module on the Business Pinterest showcase. 

ClaireH’s picture

It worked perfectly, thank you.

survivestyle’s picture

Hi everyone,

I created a mytheme.theme file in my custom theme's root folder. I pasted this function in, replaced MYTHEME with my theme name. 

The problem is, the function is output as plain text at the top of my site...

Anybody have an idea why it's doing that?

redeight’s picture

THEMENAME.theme files need to start with 

<?php
ifrik’s picture

This works for me.

Any ideas on how to also add the view mode to the suggestions?

Building websites for a better world

krzysztof domański’s picture

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"

It's a bug.

Remove duplicate suggestions block--block-content.html.twig

maskedjellybean’s picture

This really should be in core. I'm moving back towards Blocks and Block Types and away from Paragraphs and Paragraph Types now that we have Layout Builder but ran into this issue. Paragraph Types have template suggestions by default but you have to go out of your way to figure out how get template suggestions based on Block Type. The fact this isn't in core encourages people to work in a non-component based way, which I think is the opposite direction we should be pushing people.

heyphord’s picture

Though Jeff Burnz Suggestion work. You can easily theme your custom block with Display suite.

With Display suite installed, you can select the layout type in the manage fields section of your custom block. When you select the layout type, you'll be given template naming suggestions and where the original template resides.

Copy and paste in your sub-theme And Edit the theme to your heart's content. :)

r_h-l’s picture

As of D8.8, You can use a template name of `block--inline-block--<custom-block-name>.html.twig` with no extra code to add it into the suggestions. This is using Layout Builder to place the custom blocks. 

imclean’s picture

The custom block name was always included within the template suggestions. The block bundle (custom block type) still isn't.

A custom block type of "My Fancy Block" would have the template suggestion block--bundle--my-fancy-block.html.twig with this hook.