AmpVideoFormatter.php in Accelerated Mobile Pages (AMP) 8.3
File
src/Plugin/Field/FieldFormatter/AmpVideoFormatter.php
View source
<?php
namespace Drupal\amp\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Plugin\Field\FieldFormatter\GenericFileFormatter;
use Drupal\amp\AmpFormTrait;
class AmpVideoFormatter extends GenericFileFormatter {
use AmpFormTrait;
private function getLayouts() {
$options = $this
->allLayouts();
unset($options['container']);
return $options;
}
private function getLibraries() {
return [
'amp/amp.video',
];
}
public static function defaultSettings() {
return [
'height' => 175,
'width' => 350,
'layout' => 'responsive',
'autoplay' => FALSE,
'controls' => FALSE,
'loop' => FALSE,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['layout'] = $this
->layoutElement();
$form['width'] = $this
->widthElement();
$form['height'] = $this
->heightElement();
$form['autoplay'] = $this
->autoplayElement();
$form['controls'] = $this
->controlsElement();
$form['loop'] = $this
->loopElement();
$form['#prefix'] = '<div class="description">' . $this
->libraryDescription() . '</div>';
return $form;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$summary = $this
->addToSummary($summary);
return [
implode('; ', $summary),
];
}
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;
}
}