Drupal 7 tip : Get field values from entity objects like nodes, users, taxonomy, ...

Posted by: 
Dominique De Cooman

This small drupal 7 tip will help you get out values from entity objects, like nodes, users, taxonomy, ...

You probably seen this data structure when loading nodes:

<?php
$entity 
->fieldname[language][delta] = [item];
?>

You could loop through it but this will not take into account the language. There is a function that takes care of extracting data so you dont have to worry about the data structures language:

<?php
$items 
field_get_items('node'$node'field_yourfield'$node->language);
?>

You ll find more here:
http://api.drupal.org/api/drupal/modules--field--field.module/group/field/7

EDIT: This is also very a very handy function found on drupal7ish his blog http://drupal7ish.blogspot.com/2011/03/getting-field-data-out-of-entitie...

<?php
/**
 * Returns field values as actual entities where possible,
 * also allows selection of individual items to be returned
 */
function field_fetch_field_values($entity_type$entity$field_name$get_delta NULL$get_key NULL) {
  
$values = array();
  if (isset(
$entity->$field_name) && !empty($entity->$field_name)) {
    foreach (
field_get_items($entity_type$entity$field_name) as $delta => $item) {
      
$value $item;
      
$keys array_keys($item);
      if (
count($keys)==1) {
        
$key $keys[0];
        switch (
$key) {
          case 
'nid':
            
$value array_shift(entity_load('node', array($item[$key])));
            break;
          case 
'uid':
            
$value array_shift(entity_load('user', array($item[$key])));
            break;
          case 
'tid':
            
$value array_shift(entity_load('taxonomy_term', array($item[$key])));
            break;
          case 
'vid':
            
$value array_shift(entity_load('taxonomy_vocabulary', array($item[$key])));
            break;
          case 
'value':
            
$value $item['value'];
            break;
        }
      }
      else {
        if (
$get_key && isset($item[$get_key])) {
          
$value $item[$get_key];
        }
        elseif (
array_key_exists('value'$item)) {
          
$value = isset($item['safe_value']) ? $item['safe_value'] : $item['value'];
        }
      }
      
$values[$delta] = $value;
    }
  }
  if (
is_numeric($get_delta)) {
    return isset(
$values[$get_delta]) ? $values[$get_delta] : NULL;
  }
  return 
$values;
}
?>

Comments

Drupal 7 tip : Get field values from entity objects like nodes, users, taxonomy, ...

I think it is usefull to omit the language so it is calculated automatically. So even more convenient is :

<?php
$items = field_get_items('node', $node, 'field_yourfield');
?>

Drupal 7 tip : Get field values from entity objects like nodes, users, taxonomy, ...

Your right, if the current language is needed you can ommit the language parameter.

Drupal 7 tip : Get field values from entity objects like nodes, users, taxonomy, ...

Super helpful! Thank you.

Drupal 7 tip : Get field values from entity objects like nodes, users, taxonomy, ...

Good tip, will have to keep that in mind.
If you are using Entity API module, you could also use entity_metadata_wrapper() to achieve this...

Drupal 7 tip : Get field values from entity objects like nodes, users, taxonomy, ...

I have a question regarding the function found at:

http://drupal7ish.blogspot.com

It returns taxonomy term names, what change do I need to make for that function so it would return tid's instead ?!

Thanks.

Drupal 7 tip : Get field values from entity objects like nodes, users, taxonomy, ...

Thanks for noticing my little function - but there is now a module, field_extract, which provides a couple of versatile functions for fetching data in various ways from entities, you can find it here.

Add new comment