1.20.x field.api.php hook_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items)

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 to $items.

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

Related topics

File

modules/field/field.api.php, line 346
Hooks provided by the Field module.

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' => '',
        );
      }
    }
  }
}