You are here

public function UrlPlainFormatter::viewElements in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/file/src/Plugin/Field/FieldFormatter/UrlPlainFormatter.php \Drupal\file\Plugin\Field\FieldFormatter\UrlPlainFormatter::viewElements()
  2. 10 core/modules/file/src/Plugin/Field/FieldFormatter/UrlPlainFormatter.php \Drupal\file\Plugin\Field\FieldFormatter\UrlPlainFormatter::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

core/modules/file/src/Plugin/Field/FieldFormatter/UrlPlainFormatter.php, line 24

Class

UrlPlainFormatter
Plugin implementation of the 'file_url_plain' formatter.

Namespace

Drupal\file\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $file) {
    assert($file instanceof FileInterface);
    $elements[$delta] = [
      '#markup' => $file
        ->createFileUrl(),
      '#cache' => [
        'tags' => $file
          ->getCacheTags(),
      ],
    ];
  }
  return $elements;
}