0

I've been trying to render/print the data from my custom blocks using paragraphs via entity reference to my twig template. So far I've managed to test and successfully print the data to my twig using the code below

{% for item in node.field_sidebar_area %}


{{node.field_sidebar_area[loop.index0].entity.field_block_query.entity.body.value | raw }}

    {% for item in node.field_sidebar_area[loop.index0].entity.field_block_query %}

    {{ item.entity.body.value | raw }}

    {% endfor %}

{% endfor %}

If I tried using only this code below in printing the values from my custom block, it doesn't work

{% for item in node.field_sidebar_area[loop.index0].entity.field_block_query %}

{{ item.entity.body.value | raw }}

{% endfor %}

I have to set this code first before adding the for loop in order to fetch the data from the custom block

{{node.field_sidebar_area[loop.index0].entity.field_block_query.entity.body.value | raw }}
  • It can get fiddly when trying to loop through nested arrays in Twig pretty fast, IMHO. If you have a custom block you'd better prepare your render arrays there and then only pass the necessary stuff over to Twig, to keep the templates as clean and as readable as possible. – leymannx Jun 19 '18 at 6:47
  • @leymannx is there an example on how I execute your recommendation. I'm still new with Drupal 8. – clestcruz Jun 19 '18 at 6:49
  • @clestcruz, if you are new to D8 then first enable Twig debug and locate the template that outputs the markup. BTW to render a body field by hand, which you rarely need to do if you have found the correct template, never use |raw on a body field, see drupal.stackexchange.com/questions/238762/… – 4k4 Jun 19 '18 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.