Drupal 9: How to query paragraphs

By Fernando Katsukawa, 16 November, 2022

This should also work for Drupal 8 and Drupal 10 versions.

Here we will give an option to query paragraphs, which is one of the most used contrib entities used in the Drupal, very useful and key-item for some projects.

Fortunately, to query paragraphs is similar to query any other entity in Drupal 9, we must use the entityQuery, as below:

// Get all the paragraph ids which have a specific field_name value.
$paragraph_ids = \Drupal::entityQuery('paragraph')
  ->condition('type', 'paragraph_type')
  ->condition('field_name', $name)

To load the paragraphs you can use the own loadMultiple method from Paragraph entity to load multiple items, as below:

// Load multiple paragraph entities.
$paragraphs = Paragraph::loadMultiple($paragraph_ids);

// Foreach to set value and save each one.
foreach ($paragraphs as $paragraph) {
  $paragraph->set('field_active', TRUE);

About the Author

Profile picture for user fkatsukawa

Fernando Katsukawa is a certified senior backend developer, who have been worked with Drupal in the last 12 years and counting...

Saved your precious time? Use the cup below to buy me a coffee!
buy me a coffee - cup