1

I need to create a list of all submitted values from a field named field_telephone from thousands of nodes from a specific content type. using the node_load() function takes long time.

What is the fastest way to fetch the field's values?

2

The fastest (but not sanctioned/recommended) way would be to go directly in the database table to get the data:

// db_query() is quicker than db_select() so if you're going for performance,
// build up an SQL string
$sql = '
  SELECT f.entity_id, f.field_telephone_value 
  FROM {field_revision_field_telephone} f
  WHERE f.entity_type = :entity_type
  AND f.bundle = :bundle';

// Set some args up for your query
$args = array(':entity_type' => 'node', ':bundle' => 'content_type');

// Extract the query results into an array keyed by nid
$data = db_query($sql, $args)->fetchAllKeyed();

There are many good reasons not to do this kind of thing directly (no support for translations, it will only work if the field storage engine is MySQL, etc.)

Any other method I can think of would involve a call to node_load() or node_load_multiple() at some point.

1
  • you may be interested in my answer below for a non-sql way to achieve this for a set of entity ids. – tenken Jul 10 '12 at 16:49
3

You can also use field_attach_load, I believe you can pass it just 1 field you want and a set of entity ids and you can get back all the values. The below example is taken from this blog post and I take no credit for the code:

$query = new EntityFieldQuery();
 $query->entityCondition('entity_type', 'node')
   ->entityCondition('bundle', 'article')
   ->propertyCondition('status', 1)
   ->fieldCondition('field_image', 'fid', 'NULL', '!=');
 $results = $query->execute();
 $articles = $results['node'];
 $fields = field_info_instances('node', 'article');
 $field_id = $fields['field_image']['field_id'];
 field_attach_load('node', $articles, FIELD_LOAD_CURRENT, array('field_id' => $field_id));

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.