Drupal - Retrieve a field value without loading the entire node?

I don't think there's anything built in to the API but in a pinch you could just query the database directly:

$entity_type = 'node';
$bundle = 'page';
$nids = array(1, 2, 3);

$field_values = db_select('field_revision_FIELD_NAME', 'f')
  ->fields('f', array('entity_id', 'FIELD_NAME_value'))
  ->condition('entity_type', $entity_type)
  ->condition('bundle', $bundle)
  ->condition('entity_id', $nids, 'IN')
  ->condition('deleted', 0)
  ->execute()
  ->fetchAllKeyed();

After running that you should have an array of field values, keyed by their respective node's nid.

It's worth remembering the column name won't necessarily be FIELD_NAME_value; for example a node reference field would have a column name of FIELD_NAME_nid. Which one you use will depend on your field type.

UPDATE

It seems there is a way to do it with the API, but it's not pretty and still involves a manual query:

// Get the field meta data for the field_id.
$field_name = 'field_something';
$field_info = field_info_field($field_name);
$field_id = $field_info['id'];

// Load up the properties from the node table.
$nids = array(1, 2, 3);
$sql = 'SELECT * FROM {node} WHERE nid IN (:nids)';
$nodes = db_query($sql, array(':nids' => $nids))->fetchAllAssoc('nid');

// Attach the single field to all nodes.
field_attach_load('node', $nodes, FIELD_LOAD_CURRENT, array('field_id' => $field_id));

That method takes advantage of the $options parameter in field_attach_load() by specifying a field ID to load data for. It's worth noting, per the docs:

Note that returned entities may contain data for other fields, for example if they are read from a cache.

So the code might have appeared to load extra field data, but anything other than the field you've specified will come from cache.


I find a bit cleaner way using an entityCondition and field attach load.

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'story')
  ->propertyCondition('status', 1)
  ->fieldCondition('field_story_image', 'fid', 'NULL', '!=');
$result = $query->execute();

if (isset($result['node'])) {
  $stories = $result['node'];

  // At first we need to get field's id. If you already know field id, you can ommit this step
  // Get all fields attached to a given node type
  $fields = field_info_instances('node', 'story');

  // Get id of body field
  $field_id = $fields['field_story_image']['field_id'];

  // Attach a field of selected id only to get value for it
  field_attach_load('node', $stories, FIELD_LOAD_CURRENT, array('field_id' => $field_id));

  // Get values of our node field
  $output = field_get_items('node', $stories, 'field_story_image');
}

From the blog post http://timonweb.com/loading-only-one-field-from-an-entity-or-node

Tags:

Entities

7