Dominique De Cooman
Published on Dominique De Cooman (https://dominiquedecooman.com)

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

Dominique De Cooman
Monday, June 20, 2011 - 10:00
fields [1]
entities [2]
d7tip [3]

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 [4]

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... [5]

<?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;
}
?>


Source URL: https://dominiquedecooman.com/blog/drupal-7-tip-get-field-values-entity-objects-nodes-users-taxonomy

Links
[1] https://dominiquedecooman.com/blog-topics/fields-0
[2] https://dominiquedecooman.com/blog-topics/entities
[3] https://dominiquedecooman.com/blog-topics/d7tip
[4] http://api.drupal.org/api/drupal/modules--field--field.module/group/field/7
[5] http://drupal7ish.blogspot.com/2011/03/getting-field-data-out-of-entities.html