class SlickFileFormatter in Slick Carousel 7.3
Same name and namespace in other branches
- 8.2 src/Plugin/Field/FieldFormatter/SlickFileFormatter.php \Drupal\slick\Plugin\Field\FieldFormatter\SlickFileFormatter
- 8 src/Plugin/Field/FieldFormatter/SlickFileFormatter.php \Drupal\slick\Plugin\Field\FieldFormatter\SlickFileFormatter
Plugin implementation of the 'Slick File' formatter for Media integration.
Unfortunately, file entity is not based on `entity`, so no re-use.
Hierarchy
- class \Drupal\blazy\Plugin\Field\FieldFormatter\FormatterBase implements FormatterInterface
- class \Drupal\blazy\Plugin\Field\FieldFormatter\BlazyFormatterBase
- class \Drupal\slick\Plugin\Field\FieldFormatter\SlickFormatterBase
- class \Drupal\slick\Plugin\Field\FieldFormatter\SlickFileFormatter uses BlazyVideoTrait, SlickFormatterTrait
- class \Drupal\slick\Plugin\Field\FieldFormatter\SlickFormatterBase
- class \Drupal\blazy\Plugin\Field\FieldFormatter\BlazyFormatterBase
Expanded class hierarchy of SlickFileFormatter
File
- src/
Plugin/ Field/ FieldFormatter/ SlickFileFormatter.php, line 14
Namespace
Drupal\slick\Plugin\Field\FieldFormatterView source
class SlickFileFormatter extends SlickFormatterBase {
use SlickFormatterTrait;
use BlazyVideoTrait;
/**
* The blazy entity instance.
*
* @var object
*/
protected $blazyEntity;
/**
* Returns the blazy entity object.
*/
public function blazyEntity() {
if (!isset($this->blazyEntity)) {
$this->blazyEntity = new BlazyEntity($this->formatter);
}
return $this->blazyEntity;
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return SlickDefault::extendedSettings() + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function buildSettings() {
return [
'blazy' => TRUE,
] + parent::buildSettings();
}
/**
* {@inheritdoc}
*/
public function getRequiredForms() {
return [
'fieldable' => TRUE,
] + parent::getRequiredForms();
}
/**
* Build the slick carousel elements.
*/
public function buildElements(array &$build, $items) {
$settings =& $build['settings'];
$item_id = $settings['item_id'];
// Render items as is when using vanilla.
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 individual slick item.
$build['items'][$delta] = $element;
}
return;
}
// Otherwise process elements based on advanced features.
parent::buildElements($build, $items);
}
/**
* {@inheritdoc}
*/
public function buildElement(array &$element, $entity, $delta = 0) {
$settings = $element['settings'];
$item_id = $settings['item_id'];
// Layouts can be builtin, or field, if so configured.
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'];
}
// Classes, if so configured.
if (!empty($settings['class']) && isset($entity->{$settings['class']})) {
$element['settings']['class'] = $this
->blazyEntity()
->getFieldString($entity, $settings['class'], $settings);
}
// If imported Drupal\blazy\Dejavu\BlazyVideoTrait.
// Extract image item from file, and assign it to $box['item'] so that
// Blazy can display an image along with video, or just mixed.
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);
}
// Build the main stage.
// @todo $this->buildStage($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 Image rendered is picked, render image as is, a rare case.
if (!empty($settings['media_switch']) && $settings['media_switch'] == 'rendered') {
$content[] = $this
->blazyEntity()
->getFieldRenderable($entity, $settings['image'], $settings, TRUE);
}
}
// Optional image with responsive image, lazyLoad, and lightbox supports.
$element[$item_id] = isset($content) ? $content : $this
->formatter()
->getBlazy($element);
// Build caption if so configured, supports file entity/ media via $entity.
if (!empty($settings['caption'])) {
foreach ($settings['caption'] as $caption) {
if (isset($entity->{$caption}) && ($caption_content = array_filter($this
->getCaption($entity, $caption, $settings)))) {
// Put into data sepecific for fieldable entity captions where caption
// can be anything: text, link, image, etc.
$element['caption']['data'][$caption] = $caption_content;
}
}
}
// Title can be plain text, or link field.
// Field can be deleted anytime while $settings hold references, play safe.
if (!empty($settings['title']) && isset($entity->{$settings['title']})) {
if ($title = $this
->blazyEntity()
->getFieldTextOrLink($entity, $settings['title'], $settings)) {
$element['caption']['title'] = $title;
}
}
// Link, if so configured.
// Field can be deleted anytime while $settings hold references, play safe.
if (!empty($settings['link']) && isset($entity->{$settings['link']})) {
$element['caption']['link'] = $this
->blazyEntity()
->getFieldRenderable($entity, $settings['link'], $settings, TRUE);
}
// Overlay, if so configured.
// Field can be deleted anytime while $settings hold references, play safe.
if (!empty($settings['overlay']) && isset($entity->{$settings['overlay']})) {
$element['caption']['overlay'] = $this
->blazyEntity()
->getFieldRenderable($entity, $settings['overlay'], $settings, TRUE);
}
}
/**
* {@inheritdoc}
*/
protected function getCaption($entity, $field_name, $settings) {
return $this
->blazyEntity()
->getFieldRenderable($entity, $field_name, $settings, TRUE);
}
/**
* {@inheritdoc}
*/
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();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BlazyFormatterBase:: |
protected | function | Converts $items array to object for easy D8 -> D7 backports. | 1 |
BlazyFormatterBase:: |
public | function |
Implements hook_field_formatter_settings_form(). Overrides FormatterBase:: |
1 |
BlazyFormatterTrait:: |
protected | property | The blazy-related formatter service. | |
BlazyFormatterTrait:: |
protected | property | The blazy field formatter manager. | |
BlazyFormatterTrait:: |
public | function | Returns the blazy-related formatter. | |
BlazyFormatterTrait:: |
public | function | Returns the blazy service. | |
BlazyFormatterTrait:: |
public | function | ||
BlazyVideoTrait:: |
public | function | Returns video embed URL with an autoplay parameter. | |
BlazyVideoTrait:: |
public | function | Returns the host for scheme. | |
BlazyVideoTrait:: |
public | function | Gets the faked image item out of file entity, or ER, if applicable. | |
BlazyVideoTrait:: |
public | function | Gets the Media item thumbnail, or re-associate the file entity to ME. | |
BlazyVideoTrait:: |
public | function | Returns Youtube/ Vimeo video embed suitable for non-file-entity. | |
BlazyVideoTrait:: |
public | function | Returns Youtube/ Vimeo video ID from URL, thanks to Kus from s.o. | |
BlazyVideoTrait:: |
public | function | Returns video thumbnail based on video id, needed by BlazyFilter. | |
FormatterBase:: |
protected | property | The blazy-related admin formatter service. | |
FormatterBase:: |
protected | property | Whether default settings have been merged into the current $settings. | |
FormatterBase:: |
protected | property | The field definition. | |
FormatterBase:: |
protected | property | The field display. | |
FormatterBase:: |
protected | property | The field instance. | |
FormatterBase:: |
protected | property | The form settings. | |
FormatterBase:: |
protected | property | True if the field cardinality equals -1. | |
FormatterBase:: |
protected | property | The known hard-coded entities. | |
FormatterBase:: |
protected | property | The langcode. | |
FormatterBase:: |
protected | property | The blazy formatter plugin id. | |
FormatterBase:: |
protected | property | The formatter settings. | |
FormatterBase:: |
protected | property | The view mode. | |
FormatterBase:: |
public | function | Implements hook_field_formatter_settings_form(). | |
FormatterBase:: |
public | function | ||
FormatterBase:: |
public | function | Gets html settings. | |
FormatterBase:: |
public | function | Gets formatter plugin id. | |
FormatterBase:: |
public | function | ||
FormatterBase:: |
public | function | Gets formatter settings. | |
FormatterBase:: |
protected | function | Merges default settings values into $settings. | |
FormatterBase:: |
public | function | Sets html settings. | |
FormatterBase:: |
public | function | ||
FormatterBase:: |
public | function | Sets formatter settings. | |
FormatterBase:: |
protected | function | Setup common variables across different hooks. | |
FormatterBase:: |
public | function | Implements hook_field_formatter_view(). | |
SlickFileFormatter:: |
protected | property | The blazy entity instance. | |
SlickFileFormatter:: |
public | function | Returns the blazy entity object. | |
SlickFileFormatter:: |
public | function |
Build individual item if so configured such as for file entity goodness. Overrides BlazyFormatterBase:: |
|
SlickFileFormatter:: |
public | function |
Build the slick carousel elements. Overrides SlickFormatterBase:: |
|
SlickFileFormatter:: |
public | function |
Builds the settings. Overrides FormatterBase:: |
|
SlickFileFormatter:: |
public static | function |
Returns default settings. Overrides SlickFormatterBase:: |
|
SlickFileFormatter:: |
protected | function |
Build caption element if so configured can be used for thumbnail caption. Overrides SlickFormatterBase:: |
|
SlickFileFormatter:: |
public | function |
Returns required form elements for the current formatter. Overrides BlazyFormatterBase:: |
|
SlickFileFormatter:: |
public | function |
Defines the scope for the form elements. Overrides SlickFormatterBase:: |
|
SlickFormatterBase:: |
public | function |
Constructs a SlickImageFormatter instance. Overrides FormatterBase:: |
|
SlickFormatterTrait:: |
public | function | Returns the slick admin service. | |
SlickFormatterTrait:: |
public | function | 1 |