View source
<?php
namespace Drupal\amp\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter;
class AmpImageFormatter extends ImageFormatter {
public static function defaultSettings() {
return array(
'amp_layout' => 'responsive',
'amp_fixed_height' => '300',
) + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$layout_url = 'https://www.ampproject.org/docs/guides/responsive/control_layout.html#size-and-position-elements';
$element['amp_layout'] = [
'#title' => t('AMP Layout'),
'#type' => 'select',
'#default_value' => $this
->getSetting('amp_layout'),
'#empty_option' => t('None (no layout)'),
'#options' => $this
->getLayouts(),
'#description' => $this
->t('<a href=":url" target="_blank">Layout Information</a>', array(
':url' => $layout_url,
)),
];
$element['amp_fixed_height'] = array(
'#type' => 'textfield',
'#title' => t('Layout Height (used for fixed-height only)'),
'#states' => array(
'visible' => array(
':input[name="fields[' . $this->fieldDefinition
->getName() . '][settings_edit_form][settings][amp_layout]"]' => array(
'value' => 'fixed-height',
),
),
),
'#size' => 10,
'#default_value' => $this
->getSetting('amp_fixed_height'),
);
return $element;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$layout_options = $this
->getLayouts();
$layout_setting = $this
->getSetting('amp_layout');
if (isset($layout_options[$layout_setting])) {
$summary[] = t('Layout: @setting', array(
'@setting' => $layout_options[$layout_setting],
));
if ($layout_options[$layout_setting] === 'fixed-height') {
$summary[] = t('Fixed height: @height', array(
'@height' => $this
->getSetting('amp_fixed_height'),
));
}
}
return $summary;
}
private function getLayouts() {
return [
'nodisplay' => 'nodisplay',
'fixed' => 'fixed',
'responsive' => 'responsive',
'fixed-height' => 'fixed-height',
'fill' => 'fill',
'container' => 'container',
];
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
foreach ($elements as $delta => $element) {
$elements[$delta]['#item_attributes']['layout'] = $this
->getSetting('amp_layout');
if ($this
->getSetting('amp_layout') == 'fixed-height') {
$elements[$delta]['#item_attributes']['height'] = $this
->getSetting('amp_fixed_height');
$elements[$delta]['#item_attributes']['width'] = 'auto';
}
}
return $elements;
}
}