0

I am working on a query filtering form that lists entities. I have various 'range' fields and I would like to display the min and max values. Is it possible to retrieve field values with EntityQuery?

For example I want to filter nodes whose NIDs are between 10 and 50 but I would also like to see which NID is the lowest and which is the highest so I can change my input accordingly.

Or better, imagine a commerce product price. I would like to see products whose price is between 10€ and 50€ but I would like to see what is the range that I can operate with(ie. from 0.99€ for the cheapest product, up to 150€ for the most expensive product).

I am thinking that with EQ this is possible only by having two queries - one for the min value and one for the max value, sort it by desired field, load a single entity and retrieve the value from the entity object. This solution is very ineffective(< performance) and since I am already using EQ I don't want to move back to basic queries just for this since that would make EQ obsolete for me.

0

2 Answers 2

2

For example I want to filter nodes whose NIDs are between 10 and 50 but I would also like to see which NID is the lowest and which is the highest

$result = \Drupal::entityQueryAggregate('node')
  ->aggregate('nid', 'MIN', NULL, 'nid_min')
  ->aggregate('nid', 'MAX', NULL, 'nid_max')
  ->condition('nid', [10, 50], 'BETWEEN')
  ->execute();

$result[0]['nid_min'] and $result[0]['nid_max']

0
3

No, it is not possible.

There is no API to get values of specific fields. Views also doesn't do that anymore, it always loads entities as well.

1
  • 1
    To second Berdir's answer, the performance concerns aren't as great as you would expect. In Drupal 7 and 8, the field cache prevents most of those entity loads from even needing to query the field tables when loading an entity. Using the Entitycache module will also dramatically reduce the number of queries required for loading an entity.
    – Mikey P
    Commented May 8, 2015 at 3:14

Your Answer

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