View source
<?php
namespace Drupal\slick\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Component\Utility\Xss;
use Drupal\blazy\BlazyFormatterManager;
use Drupal\blazy\Plugin\Field\FieldFormatter\BlazyFileFormatterBase;
use Drupal\slick\SlickFormatterInterface;
use Drupal\slick\SlickManagerInterface;
use Drupal\slick\SlickDefault;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class SlickFileFormatterBase extends BlazyFileFormatterBase {
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, BlazyFormatterManager $blazy_manager, SlickFormatterInterface $formatter, SlickManagerInterface $manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $blazy_manager);
$this->formatter = $formatter;
$this->manager = $manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('blazy.formatter.manager'), $container
->get('slick.formatter'), $container
->get('slick.manager'));
}
public static function defaultSettings() {
return SlickDefault::imageSettings();
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$files = $this
->getEntitiesToView($items, $langcode);
if (empty($files)) {
return [];
}
$build = [
'settings' => $this
->buildSettings(),
];
$this->formatter
->buildSettings($build, $items);
$this
->buildElements($build, $files);
$this->formatter
->isBlazy($build['settings'], $build['items'][0]);
return $this
->manager()
->build($build);
}
public function buildElements(array &$build, $files) {
$settings =& $build['settings'];
$item_id = $settings['item_id'];
$tn_caption = empty($settings['thumbnail_caption']) ? NULL : $settings['thumbnail_caption'];
$media = method_exists($this, 'getMediaItem');
foreach ($files as $delta => $file) {
$settings['delta'] = $delta;
$settings['type'] = 'image';
$item = $file->_referringItem;
$settings['file_tags'] = $file
->getCacheTags();
$settings['uri'] = $file
->getFileUri();
$element = [
'item' => $item,
'settings' => $settings,
];
if ($media) {
if (!empty($this
->getImageItem($item))) {
$element['item'] = $this
->getImageItem($item)['item'];
$element['settings'] = array_merge($settings, $this
->getImageItem($item)['settings']);
}
$this
->getMediaItem($element, $file);
$settings = $element['settings'];
}
$element[$item_id] = $this->formatter
->getImage($element);
if (!empty($settings['caption'])) {
foreach ($settings['caption'] as $caption) {
$element['caption'][$caption] = empty($element['item']->{$caption}) ? [] : [
'#markup' => Xss::filterAdmin($element['item']->{$caption}),
];
}
}
$build['items'][$delta] = $element;
if (!empty($settings['nav'])) {
$thumb = [
'settings' => $settings,
];
$thumb[$item_id] = empty($settings['thumbnail_style']) ? [] : $this->formatter
->getThumbnail($settings, $element['item']);
$thumb['caption'] = empty($element['item']->{$tn_caption}) ? [] : [
'#markup' => Xss::filterAdmin($element['item']->{$tn_caption}),
];
$build['thumb']['items'][$delta] = $thumb;
unset($thumb);
}
unset($element);
}
}
public function getScopedFormElements() {
return [
'namespace' => 'slick',
'nav' => TRUE,
'thumb_captions' => [
'title' => $this
->t('Title'),
'alt' => $this
->t('Alt'),
],
'thumb_positions' => TRUE,
] + parent::getScopedFormElements();
}
}