function field_attach_preprocess

Populates the template variables with the available field values.

The $variables array will be populated with all the field instance values associated with the given entity type, keyed by field name; in case of translatable fields the language currently chosen for display will be selected.

Parameters

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

$element: The structured array containing the values ready for rendering.

$variables: The variables array is passed by reference and will be populated with field values.

Related topics

5 calls to field_attach_preprocess()
FieldAttachOtherTest::testFieldAttachView in drupal/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php
Test field_attach_view() and field_attach_prepare_view().
template_preprocess_comment in drupal/core/modules/comment/comment.module
Prepares variables for comment templates.
template_preprocess_node in drupal/core/modules/node/node.module
Prepares variables for node templates.
template_preprocess_taxonomy_term in drupal/core/modules/taxonomy/taxonomy.module
Prepares variables for taxonomy term templates.
template_preprocess_user in drupal/core/modules/user/user.pages.inc
Prepares variables for user templates.

File

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

Code

function field_attach_preprocess(EntityInterface $entity, $element, &$variables) {

  // Ensure we are working with a BC mode entity.
  $entity = $entity
    ->getBCEntity();
  foreach (field_info_instances($entity
    ->entityType(), $entity
    ->bundle()) as $instance) {
    $field_name = $instance['field_name'];
    if (isset($element[$field_name]['#language'])) {
      $langcode = $element[$field_name]['#language'];
      $variables[$field_name] = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : NULL;
    }
  }

  // Let other modules make changes to the $variables array.
  $context = array(
    'entity' => $entity,
    'element' => $element,
  );
  drupal_alter('field_attach_preprocess', $variables, $context);
}