1

I have a paragraph type my_paragraph which is attached to a node type my_content_type on multiple fields like field_a, field_b, field_c, field_d. I want to load only those attached to some specific fields, let's say field_a and field_c. I am using entityQuery, but open to other suggestions.

The entity/field structure is like:

my_content_type: Node
|_ field_a: Paragraph (type: my_paragraph)
|_ field_b: Paragraph (type: my_paragraph)
|_ field_c: Paragraph (type: my_paragraph)
|_ field_d: Paragraph (type: my_paragraph)

My code which loads all paragraphs of type:

\Drupal::entityQuery('paragraph')
  ->condition('type', 'my_paragraph')
  // Here I want to filter by the field name on the parent entity
  ->condition('PARENT_FIELD', ['field_a', 'field_c'], 'IN')
  // Some more conditions out of context
  ->condition('out_of_context_field', $out_of_context_value)
  ->execute();

Is there any entity query condition I can use here?

3

Specifically with Paragraphs there are three extra fields directly on Paragraph level, which are accessible with EntityQuery: parent_id, parent_type (entity type like "node", not bundle name like "page"), parent_field_name

You can use those three with EQ like a normal field, e.g.

->condition('parent_field_name', ['field_a', 'field_c'], 'IN')

Notice that those three fields are only available for Paragraphs, it will not work with any other entity type.

2
  • Great, thanks! Exactly what I was looking for. Where is this documented? I googled about this for an hour and didn't find any documentation. – Елин Й. Dec 10 '20 at 12:03
  • 2
    I reckon the “documentation” will be reading the baseFieldDefinitions method of the paragraph entity type – Clive Dec 10 '20 at 12:18

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.