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;
use Drupal\amp\AmpFormTrait;
class AmpImageCarousel extends ImageFormatter {
use AmpFormTrait;
private function getLayouts() {
$options = $this
->allLayouts();
unset($options['container']);
unset($options['intrinsic']);
return $options;
}
private function getLibraries() {
return [
'amp/amp.carousel',
];
}
public static function defaultSettings() {
return [
'type' => 'slides',
'layout' => 'responsive',
'width' => '',
'height' => '',
'autoplay' => FALSE,
'controls' => FALSE,
'loop' => FALSE,
];
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['type'] = [
'#type' => 'select',
'#title' => t('Carousel type'),
'#default_value' => $this
->getSetting('type'),
'#options' => [
'carousel' => 'carousel',
'slides' => 'slides',
],
];
$form['layout'] = $this
->layoutElement();
$form['layout']['#description'] .= ' ' . $this
->t('The "carousel" type only supports the fixed, fixed-height, and nodisplay layouts. The "slides" type supports the fill, fixed, fixed-height, flex-item, nodisplay, and responsive layouts.');
$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 = NULL) {
$elements = [];
$elements['#items'] = parent::viewElements($items, $langcode);
$layout = $this
->getSetting('layout');
$width = $this
->validWidth($this
->getSetting('width'), $this
->getSetting('layout'));
$height = $this
->validHeight($this
->getSetting('height'), $this
->getSetting('layout'));
foreach ($elements['#items'] as $delta => $item) {
$elements['#items'][$delta]['#item_attributes']['layout'] = $layout;
$elements['#items'][$delta]['#item_attributes']['width'] = $width;
$elements['#items'][$delta]['#item_attributes']['height'] = $height;
$elements['#items'][$delta]['#item_attributes'] = array_filter($elements['#items'][$delta]['#item_attributes']);
$elements['#items'][$delta]['#item_attributes']['loading'] = FALSE;
}
$elements['#attributes']['type'] = $this
->getSetting('type');
$elements['#attributes']['layout'] = $layout;
$elements['#attributes']['width'] = $width;
$elements['#attributes']['height'] = $height;
$elements['#attributes']['controls'] = $this
->getSetting('controls');
$elements['#attributes']['loop'] = $this
->getSetting('loop');
$elements['#attributes'] = array_filter($elements['#attributes']);
$elements['#theme'] = 'amp_image_carousel';
$elements['#attached']['library'] = $this
->getLibraries();
return $elements;
}
}