Accessing nested paragraph fields in Drupal: a step-by-step reference

andrew fletcher
3 min readMay 9, 2024

The issue — I have a content type that includes an entity reference revisions field named field_introduction (my reference for this article). This field points to a paragraph, which itself contains a text field also named field_introduction, of the type Text (formatted, long). How can I access the content stored in this paragraph’s field_introduction field?

In Drupal, managing content structured through fields and entities allows for flexible and dynamic content architectures. One common scenario involves accessing data from a paragraph field nested within an entity reference revisions field. This guide provides a detailed walkthrough on how to efficiently retrieve data from such nested structures, specifically when working within a Drupal environment.

Understanding the Structure

Firstly, it’s crucial to understand the setup. A Drupal node with an entity reference revisions field named `field_introduction`. This field points to a paragraph entity, which itself contains a text field also named `field_introduction`. Our goal is to extract the text from this nested paragraph field.

Retrieve the node

Start by fetching the node from the current Drupal route. This ensures that you’re working with the contextually relevant node.

$node = \Drupal::routeMatch()->getParameter('node');

This line uses Drupal’s routing system to get the current node object, which is the primary subject of our operation.

Check for the field

Ensure that the node is a valid instance of `NodeInterface` and that it indeed includes the field `field_introduction`. This check is vital to prevent errors in cases where the field might not be attached to the node.

if ($node instanceof \Drupal\node\NodeInterface && $node->hasField('field_introduction')) {
$field_introduction = $node->get('field_introduction')->first();
}

Here, we fetch the first item from the entity reference revisions field, assuming it’s a single reference.

Access the paragraph entity

Upon obtaining the reference, proceed to load the referenced paragraph entity. Given that the field is an entity reference revisions field, it typically points to a specific revision of a paragraph.

if ($field_introduction) {
$paragraph = $field_introduction->entity;
if ($paragraph instanceof \Drupal\paragraphs\ParagraphInterface) {
// Paragraph entity is now accessible
}
}

Extract the data

Once you have the paragraph entity, access the nested `field_introduction` text field. Check if the field exists and is not empty before trying to read its value.

if ($paragraph && $paragraph->hasField('field_introduction') && !$paragraph->get('field_introduction')->isEmpty()) {
$introduction_text = $paragraph->get('field_introduction')->value;
}

This final step fetches the value of the text field from the paragraph entity, which is the crux of our task.

Full code integration

Combining all these steps, here is how the full implementation might look in your Drupal module

$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface && $node->hasField('field_introduction')) {
$field_introduction = $node->get('field_introduction')->first();

if ($field_introduction && ($paragraph = $field_introduction->entity) && $paragraph instanceof \Drupal\paragraphs\ParagraphInterface) {
if ($paragraph->hasField('field_introduction') && !$paragraph->get('field_introduction')->isEmpty()) {
$introduction_text = $paragraph->get('field_introduction')->value;
}
}
}

The wrap

This article covers a common but sometimes tricky process in Drupal content architecture. Remember, if your entity reference revisions field can reference multiple paragraphs, then iterate through each entity reference. Additionally, robust error handling is recommended to manage cases where fields might not be set or entities are missing.

--

--