You are here

public function FieldFormatterBase::viewElements in (Entity Reference) Field Formatters 3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/FieldFormatterBase.php \Drupal\field_formatter\Plugin\Field\FieldFormatter\FieldFormatterBase::viewElements()
  2. 8 src/Plugin/Field/FieldFormatter/FieldFormatterBase.php \Drupal\field_formatter\Plugin\Field\FieldFormatter\FieldFormatterBase::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/FieldFormatterBase.php, line 165

Class

FieldFormatterBase
Base class for field formatters.

Namespace

Drupal\field_formatter\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {

  /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
  $entities = $this
    ->getEntitiesToView($items, $langcode);
  $build = [];
  foreach ($entities as $delta => $entity) {
    $viewDisplay = $this
      ->getViewDisplay($entity
      ->bundle());
    if (!empty($viewDisplay)) {
      $entityDisplay = $viewDisplay
        ->build($entity);

      // Link to entity functionality:
      if ($this
        ->getSetting('link_to_entity') && ($entity = $items
        ->getEntity()) && $entity
        ->hasLinkTemplate('canonical') && !$entity
        ->isNew()) {
        $entityUrl = $entity
          ->toUrl('canonical', [
          'language' => $this->languageManager
            ->getLanguage($langcode),
        ]);

        /*
                  TODO: This implementation overwrites the ['#url'] value of the
                  selected field, which only works for fields what support
                  ['#url'] and has the advantage that no extra wrapper is created
                  AND links in links are prevented. Anyway this will not work for
                  all field types.
        */

        //@codingStandardsIgnoreLine
        foreach ($entityDisplay as $field_name => &$field) {
          $field[0]['#url'] = $entityUrl;
        }

        /*
                  TODO: Alternative from field_link formatter, which should
                  work for all field types but may lead to links in links
                  and additional wrapper markup.
                  $field_name = $this->fieldDefinition->getName();
                  $field_output = isset($entityDisplay[$field_name]) ?
                  $entityDisplay[$field_name] : [];
                  foreach (Element::children($field_output) as $key) {
                  $entityDisplay[$field_name][$key] = [
                  '#type' => 'link',
                  '#url' => $entityUrl,
                  '#title' => $field_output[$key],
                  ];
                  }
        */
      }
      $build[$delta] = $entityDisplay;
    }
  }
  return $build;
}