Skip to content

Instantly share code, notes, and snippets.

@lezi
Last active December 17, 2015 18:29
Show Gist options
  • Select an option

  • Save lezi/5653572 to your computer and use it in GitHub Desktop.

Select an option

Save lezi/5653572 to your computer and use it in GitHub Desktop.
/* Drupal theming: Get field values in Drupal 7
Submitted by criz on Thu, 10/06/2011 - 00:54
Drupal 7 has another array structure for storing field values in $node. There is a new array key for the language.
For example:
*/
<?php
$node->field_your_field['en'][0]['value'];
?>
This can be quite useful but makes it difficult to access the right data.
So here is a handy solution:
<?php
$items = field_get_items('node', $node, 'field_yourfield', $node->language);
?>
You can leave the $node->language argument empty. If done the current language will be called automatically:
<?php
$items = field_get_items('node', $node, 'field_yourfield');
?>
As a result you get an array like you had in Drupal 6. Cool, isn't it?
But be aware that you have to take care about sanitisation when using raw values like this!
Update: When you just want to display a single field value using field formatters (with all the provided html) and sanitation included you should use:
<?php
$items = field_get_items('node', $node, 'field_yourfield');
$output = field_view_value('node', $node, 'field_yourfield', $items[$delta]);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment