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.
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.
modules/field/field.module, line 570
<?php
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.
$display = _field_info_prepare_instance_display($field, $display);
}
else {
// When using a view mode, make sure we have settings for it, or fall
// back to the 'full' view mode.
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
$instance = field_info_instance($entity_type, $field_name, $bundle);
if (!isset($instance['display'][$display])) {
$display = 'full';
}
}
// Hook invocations are done through the _field_invoke() functions in
// 'single field' mode, to reuse the language fallback logic.
$options = array('field_name' => $field_name, 'language' => field_multilingual_valid_language($langcode, FALSE));
$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 tet other modules alter the
// renderable array, as in a full field_attach_view() execution.
$context = array(
'obj_type' => $entity_type,
'object' => $entity,
'view_mode' => '_custom',
'langcode' => $langcode,
);
drupal_alter('field_attach_view', $result, $context);
if (isset($result[$field_name])) {
$output = $result[$field_name];
$output['#attached']['css'][] = drupal_get_path('module', 'field') . '/theme/field.css';
}
}
return $output;
}
?>