function field_attach_prepare_view

Prepares 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 optimization 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 in $entities; e.g. 'node' or 'user'.

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

array $displays: An array of entity display objects, keyed by bundle name.

$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_method() for details.

Related topics

7 calls to field_attach_prepare_view()
DatetimeFieldTest::renderTestEntity in drupal/core/modules/datetime/lib/Drupal/datetime/Tests/DatetimeFieldTest.php
Renders a entity_test and sets the output in the internal browser.
EntityRenderController::buildContent in drupal/core/lib/Drupal/Core/Entity/EntityRenderController.php
Implements \Drupal\Core\Entity\EntityRenderControllerInterface::buildContent().
FieldAttachOtherTest::testFieldAttachPrepareViewMultiple in drupal/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php
Tests the 'multiple entity' behavior of field_attach_prepare_view().
FieldAttachOtherTest::testFieldAttachView in drupal/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php
Test field_attach_view() and field_attach_prepare_view().
LinkFieldTest::renderTestEntity in drupal/core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php
Renders a test_entity and sets the output in the internal browser.

... See full list

File

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

Code

function field_attach_prepare_view($entity_type, array $entities, array $displays, $langcode = NULL, array $options = array()) {
  $options['langcode'] = 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)) {

      // Ensure we are working with a BC mode entity.
      $entity = $entity
        ->getBCEntity();

      // Add this entity to the items to be prepared.
      $prepare[$id] = $entity;

      // Determine the actual language code to display for each field, given the
      // language codes available in the field data.
      $options['langcode'][$id] = field_language($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. For each
  // instance, call the prepareView() method on the formatter object handed by
  // the entity display.
  $target_function = function ($instance) use ($displays) {
    if (isset($displays[$instance['bundle']])) {
      return $displays[$instance['bundle']]
        ->getFormatter($instance['field_name']);
    }
  };
  field_invoke_method_multiple('prepareView', $target_function, $prepare, $null, $null, $options);
}