function hook_field_prepare_view

Prepare field values prior to display.

This hook is invoked before the field values are handed to formatters for display, and runs before the formatters' own hook_field_formatter_prepare_view().

Unlike most other field hooks, this hook operates on multiple entities. The $entities, $instances and $items parameters are arrays keyed by entity ID. For performance reasons, information for all available entities should be loaded in a single query where possible.

Make changes or additions to field values by altering the $items parameter by reference. There is no return value.

Parameters

$entity_type: The type of $entity.

$entities: Array of entities being displayed, keyed by entity ID.

$field: The field structure for the operation.

$instances: Array of instance structures for $field for each entity, keyed by entity ID.

$langcode: The language associated with $items.

$items: $entity->{$field['field_name']}, or an empty array if unset.

Related topics

3 functions implement hook_field_prepare_view()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

file_field_prepare_view in drupal/core/modules/file/file.field.inc
Implements hook_field_prepare_view().
image_field_prepare_view in drupal/core/modules/image/image.field.inc
Implements hook_field_prepare_view().
link_field_prepare_view in drupal/core/modules/field/modules/link/link.module
Implements hook_field_prepare_view().
2 invocations of hook_field_prepare_view()
field_attach_prepare_view in drupal/core/modules/field/field.attach.inc
Prepares field data prior to display.
field_view_field in drupal/core/modules/field/field.module
Returns a renderable array for the value of a single field in an entity.

File

drupal/core/modules/field/field.api.php, line 345

Code

function hook_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {

  // Sample code from image.module: if there are no images specified at all,
  // use the default image.
  foreach ($entities as $id => $entity) {
    if (empty($items[$id]) && $field['settings']['default_image']) {
      if ($file = file_load($field['settings']['default_image'])) {
        $items[$id][0] = (array) $file + array(
          'is_default' => TRUE,
          'alt' => '',
          'title' => '',
        );
      }
    }
  }
}