1.20.x field.attach.inc field_attach_prepare_view($entity_type, $entities, $view_mode, $langcode = NULL, $options = array())

Prepare field data prior to display.

This function lets field types and formatters load additional data needed for display that is not automatically loaded during field_attach_load(). It accepts an array of entities to allow query optimisation when displaying lists of entities.

field_attach_prepare_view() and field_attach_view() are two halves of the same operation. It is safe to call field_attach_prepare_view() multiple times on the same entity before calling field_attach_view() on it, but calling any Field API operation on an entity between passing that entity to these two functions may yield incorrect results.

Parameters

$entity_type: The type of $entities; e.g. 'node' or 'user'.

$entities: An array of entities, keyed by entity id.

$view_mode: Display mode, e.g. 'full' or 'teaser'.

$langcode: (Optional) The language the field values are to be shown in. If no language is provided the current language is used.

array $options: An associative array of additional options. See _field_invoke() for details.

Related topics

File

modules/field/field.attach.inc, line 1087
Field attach API, allowing entities (nodes, users, ...) to be 'fieldable'.

Code

function field_attach_prepare_view($entity_type, $entities, $view_mode, $langcode = NULL, $options = array()) {
  // Validate $options since this is a new parameter added after Drupal 7 was
  // released.
  $options = is_array($options) ? $options : array();

  $options['language'] = array();

  // To ensure hooks are only run once per entity, only process items without
  // the _field_view_prepared flag.
  // @todo: resolve this more generally for both entity and field level hooks.
  $prepare = array();
  foreach ($entities as $id => $entity) {
    if (empty($entity->_field_view_prepared)) {
      // Add this entity to the items to be prepared.
      $prepare[$id] = $entity;

      // Determine the actual language to display for each field, given the
      // languages available in the field data.
      $options['language'][$id] = field_language($entity_type, $entity, NULL, $langcode);

      // Mark this item as prepared.
      $entity->_field_view_prepared = TRUE;
    }
  }

  $null = NULL;
  // First let the field types do their preparation.
  _field_invoke_multiple('prepare_view', $entity_type, $prepare, $null, $null, $options);
  // Then let the formatters do their own specific massaging.
  // field_default_prepare_view() takes care of dispatching to the correct
  // formatters according to the display settings for the display mode.
  _field_invoke_multiple_default('prepare_view', $entity_type, $prepare, $view_mode, $null, $options);
}