You are here

public function VideoJsPlayerListFormatter::viewElements in Video.js (HTML5 Video Player) 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 VideoJsPlayerFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/VideoJsPlayerListFormatter.php, line 39
Contains \Drupal\videojs\Plugin\Field\FieldFormatter\VideoJsPlayerListFormatter.

Class

VideoJsPlayerListFormatter
Plugin implementation of the 'videojs_player_list' formatter.

Namespace

Drupal\videojs\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.
  $video_items = array();
  foreach ($files as $delta => $file) {
    $video_uri = $file
      ->getFileUri();
    $video_items[] = Url::fromUri(file_create_url($video_uri));
  }
  $elements[] = array(
    '#theme' => 'videojs',
    '#items' => $video_items,
    '#player_attributes' => $this
      ->getSettings(),
    '#attached' => array(
      'library' => array(
        'videojs/videojs',
      ),
    ),
  );
  return $elements;
}