public function FormatterBase::view

Builds a renderable array for one field on one entity instance.

Parameters

Drupal\Core\Entity\EntityInterface $entity: The entity being displayed.

string $langcode: The language associated with $items.

array $items: Array of field values already loaded for the entities, keyed by entity id.

Return value

array A renderable array for a themed field with its label and all its values.

Overrides FormatterInterface::view

File

drupal/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php, line 83
Definition of Drupal\field\Plugin\Type\Formatter\FormatterBase.

Class

FormatterBase
Base class for 'Field formatter' plugin implementations.

Namespace

Drupal\field\Plugin\Type\Formatter

Code

public function view(EntityInterface $entity, $langcode, array $items) {
  $field = $this->field;
  $instance = $this->instance;
  $addition = array();
  $elements = $this
    ->viewElements($entity, $langcode, $items);
  if ($elements) {
    $entity_type = $entity
      ->entityType();
    $info = array(
      '#theme' => 'field',
      '#title' => $instance['label'],
      '#access' => field_access('view', $field, $entity
        ->entityType(), $entity),
      '#label_display' => $this->label,
      '#view_mode' => $this->viewMode,
      '#language' => $langcode,
      '#field_name' => $field['field_name'],
      '#field_type' => $field['type'],
      '#field_translatable' => $field['translatable'],
      '#entity_type' => $entity_type,
      '#bundle' => $entity
        ->bundle(),
      '#object' => $entity,
      '#items' => $items,
      '#formatter' => $this
        ->getPluginId(),
    );
    $addition[$field['field_name']] = array_merge($info, $elements);
  }
  return $addition;
}