I'm writing a twig template for a paragraph. I'd like to access a text field value from the page/node where this type of paragraph will be added.

Specifically, I'd like to access the value of a text field titled 'field_runner_id'. I access this value in my page/node twig template like this:


I've tried a handful of techniques (listed below) to access that field value within my paragraph template but am having no success.

How can I refer to a parent page's field value within a paragraph template?

I've found a few posts and articles that address this issue, and am able to get details about the parent node, such as the title and nid but not fields that it contains.

Eg this post: https://mark.ie/blog/printing-values-of-a-parent-node-from-a-drupal-paragraphs-field ...helped me to get the parent page and nid via adding this within my paragraph template:

{% set parent = paragraph._referringItem.parent.parent.entity %}
{{ parent.title.value }}
{{ parent.nid.value }}

I've also tried these, among others, with no success:

{{ parent.content.field_runner_id.value }}
{{ parent.content.field_runner_id[0] }}
{{ parent.content.field_runner_id|field_value }}

I think it is easier then you think:

{% set parentNode = paragraph.parentEntity %}
<!-- parentNode is already the node object, so now you can access any field directly without going through the content object --->

{{ parentNode.field_runner_id.value }}

What will make your live also more easy in general, is to install the devel and devel_kint_extras module for development.

Then you can explore the complicated drupal object structure like this and find the right field value:


Preface: Do not use that method from that blog, there is a better method.

  1. something._referringItem is not cache safe, so better not use this inside Twig templates.
  2. something._referringItem doesn't perform any access checks
  3. paragraph._referringItem.parent.parent.entity is possibly the longest and most confusing alternative to write paragraph.parentEntity

So, now the real answer :-)

paragraph.parentEntity is returning the raw parent entity object (very similar to the paragraph variable) but it does not include a render array like content. So you neither need .content nore you need |field_value.

Instead head directly for the field value parent.field_on_hostnode.0.value

More info about variables in entity templates:

content = complete render array of an entity

content.field_something = render array of a single field

content.field_something|field_value = get raw field value back from a render array (requires "Twig field value" module)

entity = raw entity object (replace entity with node in node template, paragraph in paragraph template, etc).

entity.field_something.value = not recommended, will behave differently depending on field type and field cardinality

entity.field_something.getValue() = get an array of raw field values, works for all field types and all field cardinalities

entity.field_something.0.value = get the first raw data field value (for plaintext fields, numbers, emails, telephone field; will not work with links, images, entity references...)

entity.field_something.0.name_of_database_column = get the first raw data subvalue name_of_database_column of field_something (this could be format in a formatted text field, end_value in a daterange field, target_id in a entity reference field,...)

Entity reference field only:

Note that the following methods are not cache-safe, not language-aware and do not perform access checks.

entity.field_reference.0.target_id = get the numeric ID of the first referenced child entity

entity.field_reference.0.entity = the first referenced child entity object (no render array!)

entity.field_reference.0.entity.getTranslation('de') = get the german translation of the first child object

Paragraphs only:

paragraph.parentEntity = the direct parent entity object (no render array!)

  • 1
    Also importantly when bypassing rendering you may lose access checks.
    – Kevin
    Sep 2 at 16:01
  • 1
    Good hint, I added this to the answer. AFAIK the same is true for any field_foo.0.entity
    – Hudri
    Sep 2 at 16:07
  • This is really helpful. Thank you @Hudri Sep 3 at 13:34

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.