public function FormatterBase::view

Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::view().

Overrides FormatterInterface::view

File

drupal/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php, line 95
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',
      '#weight' => $this->weight,
      '#title' => $instance['label'],
      '#access' => field_access('view', $field, $entity_type, $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;
}