You are here

public function AmpVideoFormatter::viewElements in Accelerated Mobile Pages (AMP) 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/AmpVideoFormatter.php \Drupal\amp\Plugin\Field\FieldFormatter\AmpVideoFormatter::viewElements()
  2. 8.2 src/Plugin/Field/FieldFormatter/AmpVideoFormatter.php \Drupal\amp\Plugin\Field\FieldFormatter\AmpVideoFormatter::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 GenericFileFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/AmpVideoFormatter.php, line 95

Class

AmpVideoFormatter
Plugin implementation of the 'amp_video' formatter.

Namespace

Drupal\amp\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $layout = $this
    ->getSetting('layout');
  $width = $this
    ->validWidth($this
    ->getSetting('width'), $this
    ->getSetting('layout'));
  $height = $this
    ->validHeight($this
    ->getSetting('height'), $this
    ->getSetting('layout'));
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $file) {
    $elements[$delta]['#theme'] = 'amp_video';
    $elements[$delta]['#attributes']['width'] = $width;
    $elements[$delta]['#attributes']['height'] = $height;
    $elements[$delta]['#attributes']['layout'] = $layout;
    $elements[$delta]['#attributes']['controls'] = $this
      ->getSetting('controls');
    $elements[$delta]['#attributes']['loop'] = $this
      ->getSetting('loop');
    $elements[$delta]['#attributes']['src'] = $file
      ->createFileUrl();
    $elements[$delta]['#cache'] = [
      'tags' => $file
        ->getCacheTags(),
    ];
  }
  $elements['#attached']['library'] = $this
    ->getLibraries();
  return $elements;
}