Skip to main content

The issue – I have a content type that includes an entity reference revisions field named field_introduction. 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 guide 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.

Related articles

Andrew Fletcher04 Apr 2025
Managing .gitignore changes
When working with Git, the .gitignore file plays a critical role in controlling which files and folders are tracked by version control. Yet, many developers are unsure when changes to .gitignore take effect and how to manage files that are already being tracked. This uncertainty can lead to...
Andrew Fletcher26 Mar 2025
How to fix the ‘Undefined function t’ error in Drupal 10 or 11 code
Upgrading to Drupal 10.4+ you might have noticed a warning in their code editor stating “Undefined function ‘t’”. While Drupal’s `t()` function remains valid in procedural code, some language analysis tools — such as Intelephense — do not automatically recognise Drupal’s global functions. This...
Andrew Fletcher17 Mar 2025
Upgrading to PHP 8.4 challenges with Drupal contrib modules
The upgrade from PHP 8.3.14 to PHP 8.4.4 presents challenges for Drupal 10.4 websites, particularly when dealing with contributed modules. While Drupal core operates seamlessly, various contrib modules have not yet been updated to accommodate changes introduced in PHP 8.4.x. This has resulted in...