7

I have a video content type. I run the following query in hook_cron:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node');
$query->entityCondition('bundle', 'video');
$query->propertyCondition('status', 1);
$results = $query->execute();

This returns number of entity object with property nid, vid and type. I want to use this object to find a value of a particular field of the video (say duration). I don't want to use node_load, because I would have done that in the first place instead of using the query. How can this be done?

5

The EntityFieldQuery Extra Fields module could solve your problem.

The query, as show below, will return an object that contains the entity id, entity type, and an object that contains entity_type, entity_id, revision_id, bundle, and the requested field. Your code would be:

$query = new EntityFieldQueryExtraFields();

$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'video')
  ->addExtraField('field_duration', 'value', 'value')
  ->propertyCondition('status', 1);

$results = $query->execute();

I am currently using this solution on a site.

|improve this answer|||||
11

It can't - an EntityFieldQuery will only ever load the entity ID, and the bundle keys.

There's no choice but to use node_load_multiple() (or the equivalent if using a different entity type).

$nodes = node_load_multiple(array_keys($results['node']));

Bear in mind that the $conditions parameter of node_load_multiple() is deprecated; in favour of EntityFieldQuery as I understand it.

You could theoretically subclass EntityFieldQuery and override the execute() method to return the fully loaded entities - but you'll also need to extend the class so it always knows what entity type it's querying for; otherwise it won't know which key to use in $results.

|improve this answer|||||
  • Thank you. Just a minor doubt. I didn't get when you say "subclass EntityFieldQuery - but you'll also need to extend the class". Isn't creating a subclass not the same as extending one? Or are you referring to something else I'm not understanding? – AjitS Jul 31 '13 at 8:08
  • Sorry, poor choice of words. I just meant you'll have to equip your subclass to unconditionally accept an entity type argument, as it'll be required to load the entity objects – Clive Jul 31 '13 at 13:03

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.