AmpVideoFormatter.php in Accelerated Mobile Pages (AMP) 8.2
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;
class AmpVideoFormatter extends GenericFileFormatter {
public static function defaultSettings() {
return array(
'amp_video_height' => 175,
'amp_video_width' => 350,
) + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$element['amp_video_height'] = array(
'#type' => 'number',
'#title' => t('Height'),
'#size' => 10,
'#default_value' => $this
->getSetting('amp_video_height'),
);
$element['amp_video_width'] = array(
'#type' => 'number',
'#title' => t('Width'),
'#size' => 10,
'#default_value' => $this
->getSetting('amp_video_width'),
);
return $element;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$height_setting = $this
->getSetting('amp_video_height');
if (isset($height_setting)) {
$summary[] = t('Height: @height' . 'px', array(
'@height' => $height_setting,
));
}
$width_setting = $this
->getSetting('amp_video_width');
if (isset($width_setting)) {
$summary[] = t('Width: @width' . 'px', array(
'@width' => $width_setting,
));
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
foreach ($elements as $delta => $element) {
$elements[$delta]['#theme'] = 'amp_video';
$elements[$delta]['#attributes']['height'] = $this
->getSetting('amp_video_height');
$elements[$delta]['#attributes']['width'] = $this
->getSetting('amp_video_width');
}
return $elements;
}
}