function field_view_field

You are here

7 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.

Parameters

$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

field_view_value()

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.

File

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

Code

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,
    );
    drupal_alter('field_attach_view', $result, $context);

    if (isset($result[$field_name])) {
      $output = $result[$field_name];
    }
  }

  return $output;
}

Comments

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.

You may take a look at these examples:

<?php

/*
* 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(
 
'label'=>'hidden',
 
'type' => 'text_summary_or_trimmed',
 
'settings'=>array('trim_length' => 150),
)));
 
  
?>

For more information, please take a look at these:

<?php
 
//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'];
  }
?>

<?php
 
//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'];
  }
?>

You can render an image field with:

<?php
// 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'))));
?>

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)

Field are uniques but what about the title ?

I have this code to display one photo from my gallery

<?php
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?

<?php
$content
['field_galeria'][0]['#image_style']='large';
print
render($content['field_galeria'][0]);
?>

In this example the favorite colors of a user:

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

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