function field_attach_prepare_view
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.
array $options: An associative array of additional options. See _field_invoke() for details.
Related topics
13 calls to 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.
File
-
modules/
field/ field.attach.inc, line 1133
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 view mode.
_field_invoke_multiple_default('prepare_view', $entity_type, $prepare, $view_mode, $null, $options);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.