View source
<?php
namespace Drupal\slick\Plugin\Field\FieldFormatter;
use Drupal\blazy\BlazyEntity;
use Drupal\blazy\Plugin\Field\FieldFormatter\BlazyVideoTrait;
use Drupal\slick\SlickDefault;
class SlickFileFormatter extends SlickFormatterBase {
use SlickFormatterTrait;
use BlazyVideoTrait;
protected $blazyEntity;
public function blazyEntity() {
if (!isset($this->blazyEntity)) {
$this->blazyEntity = new BlazyEntity($this->formatter);
}
return $this->blazyEntity;
}
public static function defaultSettings() {
return SlickDefault::extendedSettings() + parent::defaultSettings();
}
public function buildSettings() {
return [
'blazy' => TRUE,
] + parent::buildSettings();
}
public function getRequiredForms() {
return [
'fieldable' => TRUE,
] + parent::getRequiredForms();
}
public function buildElements(array &$build, $items) {
$settings =& $build['settings'];
$item_id = $settings['item_id'];
if (!empty($settings['vanilla'])) {
foreach ($items as $delta => $item) {
$settings['delta'] = $delta;
$settings['type'] = isset($item->type) ? $item->type : 'image';
$element = [
'item' => $item,
'settings' => $settings,
];
$element[$item_id] = $this
->blazyEntity()
->entityView($this->targetType, $item, $settings);
$build['items'][$delta] = $element;
}
return;
}
parent::buildElements($build, $items);
}
public function buildElement(array &$element, $entity, $delta = 0) {
$settings = $element['settings'];
$item_id = $settings['item_id'];
if (!empty($settings['layout'])) {
if (strpos($settings['layout'], 'field_') !== FALSE && isset($entity->{$settings['layout']})) {
$settings['layout'] = $this
->blazyEntity()
->getFieldString($entity, $settings['layout'], $settings);
}
$element['settings']['layout'] = $settings['layout'];
}
if (!empty($settings['class']) && isset($entity->{$settings['class']})) {
$element['settings']['class'] = $this
->blazyEntity()
->getFieldString($entity, $settings['class'], $settings);
}
if ($settings['type'] == 'video') {
if ($image = $this
->getImageItem($entity)) {
$element['item'] = $image['item'];
$element['settings'] = array_merge($element['settings'], $image['settings']);
}
$this
->getMediaItem($element, $entity);
}
if (!empty($settings['image']) && isset($entity->{$settings['image']}) && ($image = field_get_items($this->targetType, $entity, $settings['image']))) {
if (isset($image[0]) && isset($image[0]['uri'])) {
$image = (object) $image[0];
$replacement = new \stdClass();
foreach ([
'uri',
'alt',
'title',
'type',
'height',
'width',
] as $key) {
$replacement->{$key} = isset($image->{$key}) ? $image->{$key} : NULL;
}
$replacement->target_id = $image->fid;
$element['item'] = $replacement;
}
if (!empty($settings['media_switch']) && $settings['media_switch'] == 'rendered') {
$content[] = $this
->blazyEntity()
->getFieldRenderable($entity, $settings['image'], $settings, TRUE);
}
}
$element[$item_id] = isset($content) ? $content : $this
->formatter()
->getBlazy($element);
if (!empty($settings['caption'])) {
foreach ($settings['caption'] as $caption) {
if (isset($entity->{$caption}) && ($caption_content = array_filter($this
->getCaption($entity, $caption, $settings)))) {
$element['caption']['data'][$caption] = $caption_content;
}
}
}
if (!empty($settings['title']) && isset($entity->{$settings['title']})) {
if ($title = $this
->blazyEntity()
->getFieldTextOrLink($entity, $settings['title'], $settings)) {
$element['caption']['title'] = $title;
}
}
if (!empty($settings['link']) && isset($entity->{$settings['link']})) {
$element['caption']['link'] = $this
->blazyEntity()
->getFieldRenderable($entity, $settings['link'], $settings, TRUE);
}
if (!empty($settings['overlay']) && isset($entity->{$settings['overlay']})) {
$element['caption']['overlay'] = $this
->blazyEntity()
->getFieldRenderable($entity, $settings['overlay'], $settings, TRUE);
}
}
protected function getCaption($entity, $field_name, $settings) {
return $this
->blazyEntity()
->getFieldRenderable($entity, $field_name, $settings, TRUE);
}
public function getScopedFormElements() {
$bundles = $this->fieldDefinition['bundles'];
$strings = [
'text',
'list_text',
];
$strings = $this
->admin()
->getFieldOptions($this->fieldInstance, $strings, $this->targetType, $bundles);
$texts = [
'text',
'text_long',
'text_with_summary',
'link_field',
'url',
];
$texts = $this
->admin()
->getFieldOptions($this->fieldInstance, $texts, $this->targetType, $bundles);
$links = [
'text',
'link_field',
'url',
];
$links = $this
->admin()
->getFieldOptions($this->fieldInstance, $links, $this->targetType, $bundles);
return [
'captions' => $this
->admin()
->getFieldOptions($this->fieldInstance, [], $this->targetType, $bundles),
'images' => $this
->admin()
->getFieldOptions($this->fieldInstance, [
'image',
], $this->targetType, $bundles),
'multimedia' => TRUE,
'classes' => $strings,
'layouts' => $strings,
'links' => $links,
'titles' => $texts,
'thumb_captions' => $texts,
'use_view_mode' => TRUE,
'vanilla' => TRUE,
] + parent::getScopedFormElements();
}
}