Drupal tip: Find all field values for a node entity

This simple snippet programmatically locates all field values for a node entity using field_info_instances() from the Drupal API.

An example usage would be if you wanted to modify field values in template_preprocess_node() for output in node.tpl.php.

If you don't already have a $node object, look up node_load().

In Drupal 7.x:

<?php

$field_values = array();

// Return all field instances for our entity type, keyed by field name. // $node is our node object. $instances = field_info_instances('node', $node->type);

foreach ($instances as $field_name => $instance) {
  $field_values[$field_name] = $node->{$field_name};
}

print_r($field_values); 

?>

Example output:

Array
(
  [body] => Array
    (
      [und] => Array
        (
          [0] => Array
            (
              [value] => <p>Lorem ipsum dolor sit amet.</p>
              [summary] => 
              [format] => full_html
              [safe_value] => <p>Lorem ipsum dolor sit amet.</p>

              [safe_summary] => 
            )
        )

    )
  [field_article_top5] => Array
    (
      [und] => Array
        (
          [0] => Array
            (
              [value] => 5
            )

        )
    )

)