Drupal - Get only some fields with EntityFieldQuery?

Great question!

EntityFieldQuery is truly good stuff, but if you really want to get serious, you have to override the class in a custom module and add whatever behavior you need done in there.

I don't know your exact intentions, but I have referred myself to this article on the subject in the past. Credits to Neil Hastings for this well written write-up.

Look for code examples a bit into the article for nice examples of overiding.

Hope that helps, happy coding!


I found the answer for this! The Apache Solr module extends the EntityFieldQuery class in order to make it happen. It adds a new method called addExtraField.

<?php
$query = new ApachesolrAttachmentsEntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'event')
  ->propertyCondition('status', 1)
  ->fieldCondition('field_date', 'value', array('2011-03-01', '2011-03-31'), 'BETWEEN')
  ->fieldOrderBy('field_date', 'value', 'ASC')
  ->addExtraField('field_date', 'value', 'value')
  ->execute();
?>

If you don't have Apache Solr installed simply copy the code for extending that class.


You don't need to override anything or extend the class. You just need to use field_attach_load() to attach the field you want to the results from the EntityFieldQuery class.

See also Loading Only One Field From An Entity or Node in Drupal 7, a good article with examples on how to do it.

Tags:

Entities

7