field_attach_prepare_view

  1. drupal
    1. 8
    2. 7
Versions
7 – 8 field_attach_prepare_view($entity_type, $entities, $view_mode, $langcode = NULL)

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 View mode, e.g. 'full', 'teaser'...

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

Related topics

▾ 11 functions call field_attach_prepare_view()

comment_build_content in modules/comment/comment.module
Builds a structured array representing the comment's content.
comment_view_multiple in modules/comment/comment.module
Construct a drupal_render() style array from an array of loaded comments.
FieldAttachOtherTestCase::testFieldAttachPrepareViewMultiple in modules/field/tests/field.test
Tests the 'multiple entity' behavior of field_attach_prepare_view().
FieldAttachOtherTestCase::testFieldAttachView in modules/field/tests/field.test
Test field_attach_view() and field_attach_prepare_view().
node_build_content in modules/node/node.module
Builds a structured array representing the node's content.
node_preview in modules/node/node.pages.inc
Generate a node preview.
node_view_multiple in modules/node/node.module
Construct a drupal_render() style array from an array of loaded nodes.
TaxonomyTermFieldTestCase::testTaxonomyTermFieldWidgets in modules/taxonomy/taxonomy.test
Test widgets.
taxonomy_term_view in modules/taxonomy/taxonomy.module
Generate an array for rendering the given term.
UpgradePathTaxonomyTestCase::testTaxonomyUpgrade in modules/simpletest/tests/upgrade/upgrade.taxonomy.test
Basic tests for the taxonomy upgrade.
user_build_content in modules/user/user.module
Builds a structured array representing the profile content.

Code

modules/field/field.attach.inc, line 1085

<?php
function field_attach_prepare_view($entity_type, $entities, $view_mode, $langcode = NULL) {
  $options = array('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 view mode.
  _field_invoke_multiple_default('prepare_view', $entity_type, $prepare, $view_mode, $null, $options);
}
?>