1

I am building a Drupal 8 site. The site will have basic pages (nodes) that have three fields: a title, a body and a list of embedded blocks. The idea is to allow the editors to create content blocks by using the new functionality of Block Types in Drupal 8. Those blocks can be referenced and arrangaed inside the page by using an entity reference field.

I have built the necessary infrastructure and I can reference my custom blocks from the page and render them in the template. The trouble comes when I want to set a custom template for my blocks, for example I would like to have a custom template: block--image-gallery-block.html.twig that would be used when rendering blocks of type Image Gallery Block.

With Twig debug mode enabled I can see that Drupal is looking for a template for the entity reference field, and then starts to render the referenced block fields instead of the block itself, so I can not use any template for it (not even the default block.html.twig).

I've thought about using a custom template for my field and render here the block the way I like. Unfortunately my field can reference blocks of multiple types, so this solution won't work for me.

How can I make Drupal pick the proper template for the referenced blocks?

Edit: solution

I am explaining here the solution I've ended with after reading @ssibal and @4k4 answers.

  1. I've built some new content-types that will represent the blocks.
  2. I've modified my Basic Page content type to have an entity reference field that points to the blocks. This field simply references other nodes from the content types that I want (the ones I've made in the step 1).
  3. In the display options of my basic page, I choose which view mode I want to use for rendering the blocks.
  4. I use the view mode that I've set up in the step 3 to build custom templates for my block content types.
  5. Since I do not want block content types to be accessed directly with a URL, I've implemented a hook_node_access to throw a 404 error when directly loading a node of my block content types.
2

Those blocks can be referenced and arrangaed inside the page by using an entity reference field.

These "block contents" should be a different content type, since you are not using (based on your writing) the block functionality at all. After that, you can define your own display for this content type, and also create a twig template for it.

  • I've gone with this solution. I have created multiple content types that represent blocks (as many as block types I need) and modified my Basic page entity reference field to point to those block content types. For extra safety I've disabled the /node/XXXX paths for those nodes of a block type (I do not want them to be viewed directly) using a hook_node_access. Thank you very much! – crbelaus Jul 17 '16 at 10:10
  • Wise solution for using hook_node_access on that! – ssibal Jul 18 '16 at 7:31
0

Block content is not the same as Block.

Block content is a content entity

Block is a config entity.

Normally you place "block content" in a "block" to put it on your drupal site. Then you get a block template around your block content.

You are using "block content" as entity in a reference field, then this is no "block" and will not involve the block theme.

But you should be able to do the same theming in the field template of the reference field.

You can go inside the items loop and check every item, which kind of block content is inside and make your adjustments.

  • Thank you very much for your answer. I didn't know that block content entities did not use the block theme, it is much clearer for me now. – crbelaus Jul 17 '16 at 10:06
0

You can use Block Token to expose blocks as tokens, then use Token Filter to use these tokens on the node pages, so they can render the blocks. See: How to use tokens in a node?

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.