1

I would like to use the entityQuery for querying paragraphs

  • of type my_paragraph_type
  • With the field field_boolean_test set to TRUE
  • And a condition on the parent of the paragraph: field_parent must be set

Here is my try, but it is not working

Drupal::entityQuery('paragraph')
  ->condition('type', 'my_paragraph_type')
  ->exists('parent_id.entity:node.field_parent') //HERE IS THE PROBLEM
  ->condition('field_boolean_test', TRUE)
  ->execute();

The parent is a node and the paragraphs are attached to a field named field_paragraphs

2
  • 1
    AFAIK the .entity:entity_type. notation in entity queries is a feature of "real" entity reference fields. The parent_id of paragraph entities is defined as a string, not an entity reference field, so this syntax will not work by concept. You have to write a database query, with manual joining, etc
    – Hudri
    Commented Feb 12 at 10:54
  • Thank you. Any help available on how to write this kind of database query?
    – Baud
    Commented Feb 12 at 14:31

1 Answer 1

2
+50

With the entity query you can get all the node ids with your desired conditions, because the entity reference fields are deposited there. For the other direction you need to do a custom database query like the following:

$query = \Drupal::database()->select('paragraphs_item', 'pi');
$query->fields('pi', ['id']);
// Join the field which references the paragraph item to the node.
$query->join('node__field_parent', 'fpc', 'fpc.field_parent_target_id = pi.id');
// Join the field_boolean_test field from the paragraph.
$query->join('paragraph__field_boolean_test', 'fbt', 'fbt.entity_id = pi.id');
// Join the node field data table for conditions on the status for example.
$query->join('node_field_data', 'nfd', 'nfd.nid = fpc.entity_id');
// Join the field_parent field from the node.
$query->join('node_field_parent', 'nfp', 'nfp.nid = fpc.entity_id');
// Add the required conditions.
$query->condition('pi.type', 'my_paragraph_type');
$query->condition('fbt.field_boolean_test_value', TRUE);
$query->condition('nfp.field_parent_value', TRUE); // You can also leave this condition, because the join requires a database entry.
// Get the paragraph ids.
$paragraph_ids = $query->execute()->fetchCol();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.