function field_attach_view

7 field.attach.inc field_attach_view($entity_type, $entity, $view_mode, $langcode = NULL, $options = array())
8 field.attach.inc field_attach_view(EntityInterface $entity, EntityDisplay $display, $langcode = NULL, array $options = array())

Returns a renderable array for the fields on an entity.

Each field is displayed according to the display options specified in the instance definition for the given view mode.

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.

Sample structure:

array(
  'field_foo' => array(
    '#theme' => 'field',
    '#title' => the label of the field instance,
    '#label_display' => the label display mode,
    '#object' => the fieldable entity being displayed,
    '#entity_type' => the type of the entity being displayed,
    '#language' => the language of the field values being displayed,
    '#view_mode' => the view mode,
    '#field_name' => the name of the field,
    '#field_type' => the type of the field,
    '#formatter' => the name of the formatter,
    '#items' => the field values being displayed,
    // The element's children are the formatted values returned by
    // hook_field_formatter_view().
  ),
);

Parameters

Drupal\Core\Entity\EntityInterface $entity: The entity with fields to render.

\Drupal\entity\Plugin\Core\Entity\EntityDisplay $display: The entity display object.

$langcode: 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.

Return value

array A renderable array for the field values.

Related topics

10 calls to field_attach_view()
DatetimeFieldTest::renderTestEntity in core/modules/datetime/lib/Drupal/datetime/Tests/DatetimeFieldTest.php
Renders a test_entity and sets the output in the internal browser.
EmailFieldTest::testEmailField in core/modules/email/lib/Drupal/email/Tests/EmailFieldTest.php
Tests e-mail field.
EntityRenderController::buildContent in core/lib/Drupal/Core/Entity/EntityRenderController.php
Implements \Drupal\Core\Entity\EntityRenderControllerInterface::buildContent().
FieldAttachOtherTest::testFieldAttachView in core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php
Test field_attach_view() and field_attach_prepare_view().
LinkFieldTest::renderTestEntity in core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php
Renders a test_entity and sets the output in the internal browser.

... See full list

1 string reference to 'field_attach_view'
field_view_field in core/modules/field/field.module
Returns a renderable array for the value of a single field in an entity.

File

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

Code

function field_attach_view(EntityInterface $entity, EntityDisplay $display, $langcode = NULL, array $options = array()) {
  // Ensure we are working with a BC mode entity.
  $entity = $entity->getBCEntity();
  // Determine the actual language code to display for each field, given the
  // language codes available in the field data.
  $options['langcode'] = field_language($entity, NULL, $langcode);

  // For each instance, call the view() method on the formatter object handed
  // by the entity display.
  $target_function = function($instance) use ($display) {
    return $display->getFormatter($instance['field_name']);
  };
  $null = NULL;
  $output = field_invoke_method('view', $target_function, $entity, $null, $null, $options);
  // Remove the BC layer now.
  $entity = $entity->getNGEntity();

  // Let other modules alter the renderable array.
  $view_mode = $display->originalMode;
  $context = array(
    'entity' => $entity,
    'view_mode' => $view_mode,
    'display_options' => $view_mode,
    'langcode' => $langcode,
  );
  drupal_alter('field_attach_view', $output, $context);

  // Reset the _field_view_prepared flag set in field_attach_prepare_view(),
  // in case the same entity is displayed with different settings later in
  // the request.
  unset($entity->_field_view_prepared);

  return $output;
}