1

I have a node type which has a field that can contain an unlimited amount of images.

I want to retrieve a set of entities, but only entities that contain at least one image.

So far this is my query:

$result = $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'reference')
    ->propertyCondition('promote', 1)
    ->propertyOrderBy('created', 'DESC')
    ->pager(12)
    ->execute();

How should I use the extra fieldCondition to indicate I only want nodes with at least 1 value for field_images?

0

Seems like stating the obvious but you need to count the number of values in 'field_images', then you can set the condition to check that result and make sure it is greater than 0.

There's a good overview of adding expressions and conditions with the count() function on this question.

You'll also need to perform a join between 'node' and the table that holds your field values before you make your count, something like this:

$query = db_select('node', 'n');
$query->join('field_images', 'fi', 'n.nid = fi.nid');
...rest of query
0

You have to use a propertyCondition for this. A code like:

$result = $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'reference')
    ->propertyCondition('promote', 1)
    ->propertyCondition('field_images', 1)
    ->propertyOrderBy('created', 'DESC')
    ->pager(12)
    ->execute();

-would help. But I don't know whether it works with 'node' 'entity_type'.

If not, please comment below and/or edit this answer.

Thank you.

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.