1

I have a content type (say "Ranking") with following fields:

content

field_paragraph can reference any number of different paragraphs created with the Paragraphs Module: paragraphs

So when I print

{{ content.field_paragraph }}

in a template for this node (say node--ranking--full.html.twig), Drupal renders it starting from field.html.twig and iterating

for item in items

over every referenced paragraph and their twigs.

But I need to print a single referenced paragraph (like sub_ranking) somewhere else in my node--ranking--full.html.twig.

Searching through answers to similiar questions and trying to understand anything from kint() I have already produced some crazy variations, none of which worked:

{{ content.field_paragraph.entity.field_sub_ranking.value }}
{{ content.field_paragraph.0.entity.field_sub_ranking.0.value }}
{{ content.field_paragraph.0.entity.sub_ranking.0.value }}
{{ content.field_paragraph[0]['#node'].field_sub_ranking.0.value }}
{{ content.field_paragraph[0]['#node'].field_sub_ranking.value }}
{{ content.field_paragraph.0.entity.field_paragraph.0.entity.sub_ranking.0.value }}
{{ content.field_paragraph.0.entity.field_paragraph.0.entity.field_sub_ranking.0.value }}

How can I render my referenced sub_ranking in my node template?

  • 1
    This is a weird implementation! Why not split out the sub_ranking paragraph to a separate field if it needs to be separate? Anyway, I'd achieve this by using PHP, using a preprocessor like hook_preprocess_node, grab the values of field_paragraph and loop through it to find your sub_ranking paragraph. Then just render it into a different variable: $variables['sub_ranking_paragraph'] = $paragraph->view(); and you can print it in twig: {{ sub_ranking_paragraph }} – Beebee Jun 28 '18 at 15:58
  • Definitly agree with Beebee here, restructure your content instead of doing code here. If that is not possible, you could also use the Twig tweak module, which provides a {{ drupal_entity('paragraph', NUMERIC_ID) }} render operator – Hudri Jun 28 '18 at 16:03
  • Possible duplicate of How to print paragraphs in twig template – leymannx Jun 28 '18 at 20:18
  • Thank you, @Beebee. I have split it to a separate field and rendered it that way. – KrzysiekK Jun 28 '18 at 21:23

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.