5

I have a node type "group" with an entity reference field "field_things" that can reference zero or more "thing" nodes. I'd like to use entityQuery to find all "groups" that have exactly one value for field_things, and that value is a specific "thing".

I can't tell from the docs how to combine those two requirements.

  • 1
    You can't. EQ does not supprot expressions which you'd need for counting deltas. You have to use standard query. – user21641 Oct 14 '17 at 23:40
3

So it appears entityQuery() can't do this, but entityQueryAggregate() can. Below is an example that achieves the goal of the original post:

$thing_id = 101;
$q = \Drupal::entityQueryAggregate('node')
  ->condition('type', 'group')
  ->condition('field_things', $thing_id)
  ->groupBy('nid')
  ->conditionAggregate('field_things', 'COUNT', 1);
$res = $q->execute();

Running dpm($res) will show:

Array
(
  [0] => Array
    (
      [nid] => 85
      [field_things_count] => 1
    )

  [1] => Array
    (
      [nid] => 33
      [field_things_count] => 1
    )

  [2] => Array
    (
      [nid] => 57
      [field_things_count] => 1
    )

  [3] => Array
    (
      [nid] => 54
      [field_things_count] => 1
    )
)

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.