Loading Only One Field From An Entity or Node in Drupal 7
Time from time, while doing your custom Drupal code, you may want to load only one or several specific fields from a defined set of entities.
So actually you have three approaches to this:
1. Query for entity/node set and load whole entities to get desired fields data. Works, but not a performant solution.
2. Make a direct sql query and get desired fields out of your database. Works too, is the fasted in terms of performance solution, but not too flexible and portable.
3. Leverage EntityFieldQuery() and field_attach_load(). This approach is not as fast as the second, but way more faster than loading whole nodes, it is flexible and uses field caching mechanism. If you'll decide to change your database backend later in the future, let's say to MongoDB, you'll be able to switch without changing a line in your code, neat! But the biggest thing I love about the following approach is that you don't care if you field is single valued or multivalued. You'll get all the values without too much hassle.
So let's get in more details. EntityFieldQuery() allows finding entities based on entity properties (for example, node->changed), field values, and generic entity meta data (bundle, entity type, entity id, and revision ID). More details about what EntityFieldQuery() can do for you find in official docs here: http://api.drupal.org/api/drupal/includes%21entity.inc/class/EntityFieldQuery/7
Now let's imagine a sample task. We'll need a list of image urls of image fields attached to nodes of type "Story". So how do we get this list? Simple, watch my moves:
1. Let's use EntityFieldQuery() to query a list of our article nodes:
1 2 3 4 5 6
So we queried for a list of nodes of type "story" and made sure that node has image attached to it. Imagine yourself doing this query via raw sql. $result should return us a list of entity ids. Node ids should leave in $result['node']:
1 2 3
Yes, we have to check if we have $result['node'] is set, because EntityFieldQuery() returns empty array in case if didn't find any nodes.
2. Now to the most interesting part. Okay, we have an array of node ids, so now we want to load only 'field_story_images' for these nodes. We'll use field_attach_load() to do this:
1 2 3 4 5 6 7 8 9 10 11 12 13
I described what we're doing in each step in the code, so everything should be pretty much understandable. In the end, we're getting $stories array, which holds "semi-loaded" nodes with only field we queried attached. Caveat. field_attach_load() doesn't invoke hook_node_load(), so if some data is being manipulated via hook_node_load() you won't get its representation via described method. This is the only drawback of this method. But in most cases it is safe to use. Just check if it works in your situation.
3. To access your list of values you can do the way you access full node's values, something like this:
UPDATE: I wrote a more detailed tutorial on how to access node fields values: http://timonweb.com/working-with-drupal-fields-getting-single-field-values-for-display-and-manipulaton">Working with Drupal Fields: Getting Single Field Values For Display and Manipulaton. That simple! So for those who could get lost, here's the full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
UPDATE: put this snippet on dropbucket for future reference: http://dropbucket.org/node/54 As you see, the described approach is pretty neat, you get all values of a field in one go and you leverage Drupal cache mechanism. More over, if you want to query for several fields, you need to add just two more lines to our code, let's say, in addition to image fields we want to get a body field, so after:
1 2 3 4 5
you just need to add:
1 2 3 4 5
And you'll get a body field attached too! Or in more cleaner way these two fields attachment could look like:
1 2 3 4 5 6 7 8 9 10 11 12 13
Wow that was a long read, but if you've survived till the end this means that now you have a powerful tool in your skillset. Go and try it!