0

My use case requires that I get a fields values in the parent paragraph template file.

So to be more clear I have a paragraph type (wrapper) which itself has a field referencing to another paragraph type (nested).

I am trying to get the values of a field (like title) from the child paragraph (nested one) in the parent twig template.

Hope for some help with this.

3

2 Answers 2

4

in your node.html.twig file you can traverse up the element array like this

{{node.field_parent.entity.field_paragraph.entity.field_text.value}}

where

  • field_parent.entity is the parent paragraph
  • field_paragraph.entity is the child paragraph attached to parent
  • field_text is a text field attached to child paragraph

OR inside your paragraph.html.twig

{{paragraph.field_paragraph.entity.field_text.value}}

NOTE: its actually better to put this code in a paragraph--parent.html.twig file otherwise this template is going to be used also for the child text paragraph which does not have field_paragraph and its going to lead to error.

3
  • Fair enough but I need this from within a paragraph template
    – drupalhgy
    May 19, 2018 at 16:46
  • @drupalhgy updated answer
    – GiorgosK
    May 19, 2018 at 17:12
  • Note that reaching into the entity directly and grabbing the field values will break multilingual. Apr 26 at 21:44
0

I have eventually gotten this to work, this is how I accessed the field from the parent paragraph template

field_product is the paragraph field on my parent paragraph type. field_title is the field I was trying to get the value of which belonged to the child paragraph type

{{ content.field_product.0['#paragraph'].field_title.0.value }}
4
  • I'd strongly suggest using a theme_preprocess_HOOK() for your template instead and providing the title as a distinct variable to the template. Accessing complex sub paragraph fields like this is very unfriendly for themers that may have to extend the templates at a later time. May 20, 2018 at 13:48
  • Why is it unfriendly?
    – drupalhgy
    May 21, 2018 at 14:02
  • A distinct own variable as {{ product_title }} is easier to understand, isn't it? Additionally, setting this variable in a template's pre-processing gives you more control over checking, if such a product is really set by the content editors (Drupal's Twig e.g. still has some issues with checking, whether fields are really empty), or changing the data structure at a later stage of your site's lifecycle without affecting the output of your template. Moving data-access (and validation) of such a deeply nested data structure to pre-processing just keeps your templates clean. May 21, 2018 at 14:39
  • I agree, you shouldn't dig into render arrays like in this answer, render arrays are subject to change without notice, they are not an API. You can get field values in Twig though, directly from the Entity API, see the answer from @GiorgosK. But these are raw values not prepared for rendering, more useful for logic or identifiers, then sanitized with a filter like |clean_class.
    – 4uk4
    May 22, 2018 at 7:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.