Field theming functions

field_view_field
field_view_value

print render(field_view_field('user', $user, 'field_avatar', array('settings' => array('image_style' => 'post_avatar'))));

Results in user object (from user_load) + field name and settings array.
You can also remove the label.

field_view_value is the same but with one element.

Example: displaying the first image in a field with multiple images:

$image = field_get_items('node', $node, 'field_image');
$output = field_view_value('node', $node, 'field_image', $image[0]);

field_get_items is handy too. You don't have to look it up in the entire node object anymore, like:

$node->content['field_body'][0][LANGUAGE_NONE]['safe_value'];

But with field get items you don't have to woory about languages etc.

Example: hide label when field empty:
check with field_get_items if return is FALSE.

About

This is the company blog of
Drupal specialist Merge.nl

We are located in Breda (Netherlands) and build websites using Drupal. More about us.

Content on this blog is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Netherlands License.

Creative Commons License

Recent Comments

Social