You are here

public function VideoEmbedPlayerFormatter::viewElements in Video 8

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

Class

VideoEmbedPlayerFormatter
Plugin implementation of the video field formatter.

Namespace

Drupal\video\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $element = [];
  $settings = $this
    ->getSettings();
  foreach ($items as $delta => $item) {
    $file = File::load($item->target_id);
    if (!$file) {
      continue;
    }
    $metadata = isset($item->data) ? unserialize($item->data) : [];
    $scheme = file_uri_scheme($file
      ->getFileUri());
    $provider = $this->providerManager
      ->loadProviderFromStream($scheme, $file, $metadata);
    if ($provider) {
      $element[$delta] = $provider
        ->renderEmbedCode($settings);
    }
  }
  return $element;
}