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.
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:
$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.
A renderable array for the field value.
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;
}