You are here

public function FileDownloadLinkMedia::viewElements in File Download Link 8

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

modules/file_downoad_link_media/src/Plugin/Field/FieldFormatter/FileDownloadLinkMedia.php, line 212

Class

FileDownloadLinkMedia
Plugin implementation of the 'file_download_link_media' formatter.

Namespace

Drupal\file_download_link_media\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $media) {
    $media_source = $media
      ->getSource();
    $source_field = $media_source
      ->getSourceFieldDefinition($media->bundle->entity)
      ->getName();
    $elements[$delta] = $media->{$source_field}
      ->view([
      'type' => 'file_download_link',
      'label' => 'hidden',
      'settings' => $this
        ->getSettings(),
    ]);
    CacheableMetadata::createFromObject($media)
      ->applyTo($elements[$delta]);
  }
  return $elements;
}