You are here

public function VideoUrlFormatter::viewElements in Video 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/VideoUrlFormatter.php \Drupal\video\Plugin\Field\FieldFormatter\VideoUrlFormatter::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/VideoUrlFormatter.php, line 92
Contains \Drupal\video\Plugin\Field\FieldFormatter\VidePlayerFormatter.

Class

VideoUrlFormatter
Plugin implementation of the 'video_player' formatter.

Namespace

Drupal\video\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = array();
  $files = $this
    ->getEntitiesToView($items, $langcode);

  // Early opt-out if the field is empty.
  if (empty($files)) {
    return $elements;
  }

  // Collect cache tags to be added for each item in the field.
  foreach ($files as $delta => $file) {
    $video_uri = file_create_url($file
      ->getFileUri());
    $elements[$delta] = [
      '#markup' => $video_uri,
    ];
  }
  return $elements;
}