0

I drupal 8, I am trying to create a template for a 'custom block type' (called shortcut) with fields (shortcut_image, shortcut_icon). I followed the suggested template and created region--shortcut.html.twig

{% if content %}<div class="row">{{ content }}</div>{% endif %}

I want to apply grid and use the value of the field shortcut_image as background for each of the shortcut items to make it look like this http://prntscr.com/cynmba.

My problem now is that, the {{content} variable is not an array. How do i get the value of the fields to create a custom template for that specific custom block type or region?

2 Answers 2

1

You can take a look at all the variables available in the template by doing

{{ dump() }}

Make sure the template is being used by enabling twig debug (services.yml in your default folder)

You might be better off by using hook_preprocess_HOOK() (the HOOK part can be found with the twig debug enabled) and putting the values you need in separate variables which can be used in TWIG.

Sign up to request clarification or add additional context in comments.

Comments

0

Copy block.html.twig and re-name the copy to block--[yourcustomblockname]-html.twig and make your changes in that template.

To view the data structure of variables, install devel module for D8 https://www.drupal.org/project/devel and enable Devel and Devel Kint. Then in your template file, add this: {{ kint() }}. You can also use a parameter to reduce the results.

2 Comments

thanks, but i am trying to create a template for custom block type, so that i don't have to create template for each block of that type. There are also no suggested template on debug. The only closest was the template you mentioned, so maybe it's really not possible to create a template for custom block type.
You can create new template suggestions for custom block types in your .theme file of your theme. I have not tried this but here is some code for it on GitHub gist.github.com/markconroy/9daa7cc8ee95cbd87e19aef181afefb2

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.