7.x field.module field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL)

Returns a renderable array for the value of a single field in an entity.

The resulting output is a fully themed field with label and multiple values.

This function can be used by third-party modules that need to output an isolated field.

  • Do not use inside node (or any other entity) templates; use render($content[FIELD_NAME]) instead.
  • Do not use to display all fields in an entity; use field_attach_prepare_view() and field_attach_view() instead.
  • The field_view_value() function can be used to output a single formatted field value, without label or wrapping field markup.

The function takes care of invoking the prepare_view steps. It also respects field access permissions.


$entity_type: The type of $entity; e.g., 'node' or 'user'.

$entity: The entity containing the field to display. Must at least contain the id key and the field data to display.

$field_name: The name of the field to display.

$display: Can be either:

  • The name of a view mode. The field will be displayed according to the display settings specified for this view mode in the $instance definition for the field in the entity's bundle. If no display settings are found for the view mode, the settings for the 'default' view mode will be used.
  • An array of display settings, as found in the 'display' entry of $instance definitions. The following key/value pairs are allowed:

    • label: (string) Position of the label. The default 'field' theme implementation supports the values 'inline', 'above' and 'hidden'. Defaults to 'above'.
    • type: (string) The formatter to use. Defaults to the 'default_formatter' for the field type, specified in hook_field_info(). The default formatter will also be used if the requested formatter is not available.
    • settings: (array) Settings specific to the formatter. Defaults to the formatter's default settings, specified in hook_field_formatter_info().
    • weight: (float) The weight to assign to the renderable element. Defaults to 0.

$langcode: (Optional) The language the field values are to be shown in. The site's current language fallback logic will be applied no values are available for the language. If no language is provided the current language will be used.

Return value

A renderable array for the field value.

See also


Related topics

3 calls to field_view_field()
FieldDisplayAPITestCase::testFieldViewField in modules/field/tests/field.test
Test the field_view_field() function.
field_view_value in modules/field/field.module
Returns a renderable array for a single field value.
hook_tokens_alter in modules/system/system.api.php
Alter replacement values for placeholder tokens.


modules/field/field.module, line 874
Attach custom data fields to Drupal entities.


function field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL) {
  $output = array();
  if ($field = field_info_field($field_name)) {
    if (is_array($display)) {

      // When using custom display settings, fill in default values.
      $cache = _field_info_field_cache();
      $display = $cache
        ->prepareInstanceDisplay($display, $field["type"]);

    // Hook invocations are done through the _field_invoke() functions in
    // 'single field' mode, to reuse the language fallback logic.
    // Determine the actual language to display for the field, given the
    // languages available in the field data.
    $display_language = field_language($entity_type, $entity, $field_name, $langcode);
    $options = array(
      'field_name' => $field_name,
      'language' => $display_language,
    $null = NULL;

    // Invoke prepare_view steps if needed.
    if (empty($entity->_field_view_prepared)) {
      list($id) = entity_extract_ids($entity_type, $entity);

      // First let the field types do their preparation.
      _field_invoke_multiple('prepare_view', $entity_type, array(
        $id => $entity,
      ), $display, $null, $options);

      // Then let the formatters do their own specific massaging.
      _field_invoke_multiple_default('prepare_view', $entity_type, array(
        $id => $entity,
      ), $display, $null, $options);

    // Build the renderable array.
    $result = _field_invoke_default('view', $entity_type, $entity, $display, $null, $options);

    // Invoke hook_field_attach_view_alter() to let other modules alter the
    // renderable array, as in a full field_attach_view() execution.
    $context = array(
      'entity_type' => $entity_type,
      'entity' => $entity,
      'view_mode' => '_custom',
      'display' => $display,
      'language' => $langcode,
    drupal_alter('field_attach_view', $result, $context);
    if (isset($result[$field_name])) {
      $output = $result[$field_name];
  return $output;


davidwhthomas’s picture

Note, to use the render($content[FIELD_NAME]) option in a node template, the field must be set to visible in display options for that type.

yookoala’s picture

You may take a look at these examples:

 * Example 1)
 * standard output of node body
print render(field_view_field('node', $node, 'body')); 

 * Example 2)
 * output as "summary or trimmed"
 * the same as the standard teaser body output
print render(field_view_field('node', $node, 'body', array(
  'type' => 'text_summary_or_trimmed', 

 * Example 3)
 * more control over the output, include
 * 1. display 'text summary or trimmed' of the body
 * 2. hide the label 'Body' from output
 * 3. if there is no summary, trim the text to 150 characters
 *     (instead of 600 by default)
print render(field_view_field('node', $node, 'body', array(
  'type' => 'text_summary_or_trimmed', 
  'settings'=>array('trim_length' => 150),

For more information, please take a look at these:

herderwu’s picture

  //get one node's field_tags(['taxonomy_term']->name)
  //check the content type has the field
  if( !empty($vars['node']->field_tags) ) {
    $items_field_tags = field_get_items('node', $vars['node'], 'field_tags');
    $field_data_value = field_view_value('node', $vars['node'], 'field_tags', $items_field_tags[0]);
    print render($field_data_value);
    $field_data = field_view_field('node', $vars['node'], 'field_tags');
    print render($field_data);
    $vars['node_field_tags'] = $field_data['#items'];
herderwu’s picture

  //get one node's field_tags(['taxonomy_term']->name)
  //check the content type has the field
  if( !empty($vars['node']->field_tags) ) {
    $items_field_tags = field_get_items('node', $vars['node'], 'field_tags');
    $field_data_value = field_view_value('node', $vars['node'], 'field_tags', $items_field_tags[0]);
    print render($field_data_value);
    $field_data = field_view_field('node', $vars['node'], 'field_tags');
    print render($field_data);
    $vars['node_field_tags'] = $field_data['#items'];
paulhudson’s picture

You can render an image field with:

// Render a user image with the default image style
render(field_view_field('user', $user, 'field_avatar'));
// and a nodes image
render(field_view_field('node', $node, 'field_image'));
// Render with a custom image style, in this case named 'post_avatar'
render(field_view_field('user', $user, 'field_avatar', array('settings' => array('image_style' => 'post_avatar'))));
emarchak’s picture

The above example will throw you a pass by reference error if you have reporting on, as render expects a variable it can manipulate. The following would work fine and not log any errors:

// Render a user image with the default image style
$user_image = field_view_field('user', $user, 'field_avatar');
// and a nodes image
$node_image = field_view_field('node', $node, 'field_image')
// Render with a custom image style, in this case named 'post_avatar'
$custom_image = field_view_field('user', $user, 'field_avatar', array('settings' => array('image_style' => 'post_avatar')));
yurtboy’s picture

I needed to output the taxonomy terms field that had the image. In this case it was a Panel Page and I made a custom pane.
To get the data I just used the $context->data which is the same as the term object and applied this code

$image = field_view_field('taxonomy_term', $context->data, 'field_department_image', $display = array(), $langcode = NULL);

So the taxonomy_term was the entity type and field_department_image was the field type I wanted from that object.

In this case I passed it to the panes rendering $block->content = $image output but really it was just a renderable array.

#theme (String, 5 characters ) field
#weight (Integer) 0
#title (String, 5 characters ) Image
#access (Boolean) TRUE
#label_display (String, 5 characters ) above
#view_mode (String, 15 characters ) _custom_display
#language (String, 3 characters ) und
#field_name (String, 22 characters ) field_department_image
#field_type (String, 5 characters ) image
#field_translatable (String, 1 characters ) 0
#entity_type (String, 13 characters ) taxonomy_term
#bundle (String, 15 characters ) department_name
#object (Object) stdClass
#items (Array, 1 element)
#formatter (String, 5 characters ) image
0 (Array, 4 elements)
Jibus’s picture

Field are uniques but what about the title ?

Paracetamol’s picture

I struggled with this for a moment, but as the title is no real 'field', you output its value like this:

echo render($node->title);

miyek’s picture

I have this code to display one photo from my gallery

print render($content['field_galeria'][0] );

I would like to change display format from 'thumbnail' to 'large' (but i won't to change this to all images from my custom 'field_galeria', but only one. Can any one help?

sylv3st3r’s picture

print render($content['field_galeria'][0]);
Scott Weston’s picture

In this example the favorite colors of a user:

$account = user_load($some_user_id);
$fav_colors = field_view_field('user', $account, 'field_favorite_colors', array('type' => 'taxonomy_term_reference_plain'));

This will output the user's favorite colors from this taxonomy term reference field as a bulleted list with the names as plaintext.

holyfire’s picture

If you are using a custom formatter you'll want to use something like the following:

$display = array('type' => 'custom_formatters_my_formatter');
$output = field_view_field('node', $node, 'field_foo', $display);

This came up for me in outputting a specific taxonomy formatted with commas.

Real world example used in a preprocess-page.inc file.

$display = array('type' => 'custom_formatters_custom_tag_term_link');
$output_dek .= render(field_view_field('node', $vars['node'], 'field_tags", $display));
nicxvan’s picture

It seems that this is flexible enough to use both versions of display: When you have a multivalue field you need to specify which field you are looking for before the display.
field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL)
Should be something like:
field_view_field($entity_type, $entity, $field_name, multivalue[X], $display = array(), $langcode = NULL)

Real world example with a content type class that has a multivalue date field called sessions.

$date_output = field_view_value('node', $class, 'field_sessions', $sessions_values[$a_session], array('settings' => array('format_type' => 'short')));

$class is the loaded node;
$session_values is field_get_items array;
$a_session is just a foreach that loops through 0-x;

thomas.lobjoie’s picture

We searched for a wee while, so if it can help people... To render a entity reference field via field_view_field, here is the way:

$sub_pages = field_view_field('node', $node, 'field_sub_page', array('label'=>'hidden', 'type' => 'entityreference_entity_view', 'settings' => array('view_mode' => 'full')));


print render($sub_pages);

Alex72RM’s picture

Thank you very much! I've searched deeply... but just your solution is a complete one ;)

thomas.lobjoie’s picture

And this how to render a file from the file entity and specify which view mode you want to render:

$field_medias_images = field_view_field('node', $node, 'field_medias_images', array('label'=>'hidden', 'type' => 'file_rendered', 'settings' => array('file_view_mode' => 'full')));

print drupal_render($field_medias_images);

mlncn’s picture

manu manu’s picture

Actually not correct: The SO thread is about rendering a field without an entity.

With the hosting entity, this is a simple as:

$rendered_field = $node->{$field_name}->view('full');
isaachorton’s picture

What is the solution for rendering images and image parameters like a URL, URI to a page template, ie page.tpl.php when they come from an entity type created by a module(Group Module)? I cant figure this out for the life of me and Ive been trying for a few days now... Im using Drupal 7.

asghar’s picture

If you want to display date field in specific format you can use below snippet:

$time = render(field_view_field("node", $node, 'field_date', array(
  'type' => 'date_default', // Field Formatter 
  'label' => 'hidden', // Don't want to display label.
  'settings' => array(
    'format_type' => 'emed_time', // date format like short, long or custom 

DrupalViator’s picture

To access fields in a profile using the module Profile 2, use "profile2" as $entity_type.