function FormatterBase::view

Same name in other branches
  1. 9 core/lib/Drupal/Core/Field/FormatterBase.php \Drupal\Core\Field\FormatterBase::view()
  2. 8.9.x core/lib/Drupal/Core/Field/FormatterBase.php \Drupal\Core\Field\FormatterBase::view()
  3. 10 core/lib/Drupal/Core/Field/FormatterBase.php \Drupal\Core\Field\FormatterBase::view()

Overrides FormatterInterface::view

1 call to FormatterBase::view()
EntityReferenceFormatterBase::view in core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php
1 method overrides FormatterBase::view()
EntityReferenceFormatterBase::view in core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php

File

core/lib/Drupal/Core/Field/FormatterBase.php, line 86

Class

FormatterBase
Base class for 'Field formatter' plugin implementations.

Namespace

Drupal\Core\Field

Code

public function view(FieldItemListInterface $items, $langcode = NULL) {
    // Default the language to the current content language.
    if (empty($langcode)) {
        $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
            ->getId();
    }
    $elements = $this->viewElements($items, $langcode);
    // Field item lists, in particular for computed fields, may carry cacheable
    // metadata which must be bubbled.
    if ($items instanceof CacheableDependencyInterface) {
        (new CacheableMetadata())->addCacheableDependency($items)
            ->applyTo($elements);
    }
    // If there are actual renderable children, use #theme => field, otherwise,
    // let cacheability metadata pass through for correct bubbling.
    if (Element::children($elements)) {
        $entity = $items->getEntity();
        $entity_type = $entity->getEntityTypeId();
        $field_name = $this->fieldDefinition
            ->getName();
        $info = [
            '#theme' => 'field',
            '#title' => $this->fieldDefinition
                ->getLabel(),
            '#label_display' => $this->label,
            '#view_mode' => $this->viewMode,
            '#language' => $items->getLangcode(),
            '#field_name' => $field_name,
            '#field_type' => $this->fieldDefinition
                ->getType(),
            '#field_translatable' => $this->fieldDefinition
                ->isTranslatable(),
            '#entity_type' => $entity_type,
            '#bundle' => $entity->bundle(),
            '#object' => $entity,
            '#items' => $items,
            '#formatter' => $this->getPluginId(),
            '#is_multiple' => $this->fieldDefinition
                ->getFieldStorageDefinition()
                ->isMultiple(),
            '#third_party_settings' => $this->getThirdPartySettings(),
        ];
        $elements = array_merge($info, $elements);
    }
    return $elements;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.