View source
<?php
namespace Drupal\blazy\Dejavu;
use Drupal\Component\Utility\Xss;
use Drupal\Component\Utility\Html;
trait BlazyStylePluginTrait {
public function getDefinedFieldOptions($definitions = []) {
$field_names = $this->displayHandler
->getFieldLabels();
$definition = [];
$stages = [
'blazy_media',
'block_field',
'colorbox',
'entity_reference_entity_view',
'gridstack_file',
'gridstack_media',
'photobox',
'video_embed_field_video',
'youtube_video',
];
$options = [];
foreach ($this->displayHandler
->getOption('fields') as $field => $handler) {
if (isset($handler['type'])) {
switch ($handler['type']) {
case 'blazy':
case 'image':
case 'media':
case 'media_thumbnail':
case 'intense':
case 'responsive_image':
case 'video_embed_field_thumbnail':
case 'video_embed_field_colorbox':
case 'youtube_thumbnail':
$options['images'][$field] = $field_names[$field];
$options['overlays'][$field] = $field_names[$field];
$options['thumbnails'][$field] = $field_names[$field];
break;
case 'list_key':
$options['layouts'][$field] = $field_names[$field];
break;
case 'entity_reference_label':
case 'text':
case 'string':
case 'link':
$options['links'][$field] = $field_names[$field];
$options['titles'][$field] = $field_names[$field];
if ($handler['type'] != 'link') {
$options['thumb_captions'][$field] = $field_names[$field];
}
break;
}
$classes = [
'list_key',
'entity_reference_label',
'text',
'string',
];
if (in_array($handler['type'], $classes)) {
$options['classes'][$field] = $field_names[$field];
}
$slicks = strpos($handler['type'], 'slick') !== FALSE;
if ($slicks || in_array($handler['type'], $stages)) {
$options['overlays'][$field] = $field_names[$field];
}
if (in_array($handler['type'], $stages)) {
$options['images'][$field] = $field_names[$field];
}
}
if (isset($handler['field'])) {
if ($handler['field'] == 'title') {
$options['classes'][$field] = $field_names[$field];
$options['titles'][$field] = $field_names[$field];
$options['thumb_captions'][$field] = $field_names[$field];
}
if (in_array($handler['field'], [
'nid',
'nothing',
'view_node',
])) {
$options['links'][$field] = $field_names[$field];
$options['titles'][$field] = $field_names[$field];
}
$blazies = strpos($handler['field'], 'blazy_') !== FALSE;
if ($blazies) {
$options['images'][$field] = $field_names[$field];
$options['overlays'][$field] = $field_names[$field];
$options['thumbnails'][$field] = $field_names[$field];
}
}
$options['captions'][$field] = $field_names[$field];
}
$definition['plugin_id'] = $this
->getPluginId();
$definition['settings'] = $this->options;
$definition['current_view_mode'] = $this->view->current_display;
foreach ($definitions as $key) {
$definition[$key] = isset($options[$key]) ? $options[$key] : [];
}
$contexts = [
'handler' => $this->displayHandler,
'view' => $this->view,
];
$this->blazyManager
->getModuleHandler()
->alter('blazy_views_field_options', $definition, $contexts);
return $definition;
}
public function getImageRenderable(array &$settings, $row, $index) {
$image = $this
->isImageRenderable($row, $index, $settings['image']);
if (empty($image['raw'])) {
return $image;
}
if ($item = $this
->getImageItem($image)) {
if (empty($settings['image_style'])) {
$image_style = isset($image['rendered']['#image_style']) ? $image['rendered']['#image_style'] : '';
$settings['image_style'] = empty($settings['image_style']) ? $image_style : $settings['image_style'];
}
$theme = isset($image['rendered']['#theme']) ? $image['rendered']['#theme'] : '';
if (in_array($theme, [
'blazy',
'image_formatter',
])) {
$settings['cache_tags'] = isset($image['rendered']['#cache']['tags']) ? $image['rendered']['#cache']['tags'] : [];
if ($theme == 'blazy') {
$item_settings = array_filter($image['rendered']['#build']['settings']);
$settings = array_filter($settings);
$settings = array_merge($item_settings, $settings);
}
elseif ($theme == 'image_formatter') {
$settings['content_url'] = isset($image['rendered']['#url']) ? $image['rendered']['#url'] : '';
if (empty($settings['media_switch']) && !empty($settings['content_url'])) {
$settings['media_switch'] = 'content';
}
}
$build = [
'item' => $item,
'settings' => $settings,
];
$image['rendered'] = $this->blazyManager
->getImage($build);
}
}
return $image;
}
public function isImageRenderable($row, $index, $field_image = '') {
if (!empty($field_image) && ($image = $this
->getFieldRenderable($row, $index, $field_image))) {
if ($this
->getImageItem($image)) {
return $image;
}
if (isset($image['rendered'])) {
return $image;
}
}
return [];
}
public function getImageItem($image) {
$item = [];
if (isset($image['raw'])) {
$item = empty($image['rendered']['#item']) ? [] : $image['rendered']['#item'];
if (isset($image['rendered']['#build'])) {
$item = $image['rendered']['#build']['item'];
}
}
if (!is_object($item)) {
return [];
}
return $item;
}
public function getCaption($index, $settings = []) {
$items = [];
$keys = array_keys($this->view->field);
if (!empty($settings['caption'])) {
$caption_items = [];
foreach ($settings['caption'] as $key => $caption) {
$caption_rendered = $this
->getField($index, $caption);
if (empty($caption_rendered)) {
continue;
}
if (in_array($caption, array_values($keys))) {
$caption_items[$key]['#markup'] = $caption_rendered;
}
}
$items['data'] = $caption_items;
}
$items['link'] = empty($settings['link']) ? [] : $this
->getFieldRendered($index, $settings['link']);
$items['title'] = empty($settings['title']) ? [] : $this
->getFieldRendered($index, $settings['title'], TRUE);
if (!empty($settings['overlay'])) {
$items['overlay'] = $this
->getFieldRendered($index, $settings['overlay']);
}
return $items;
}
public function getLayout(array &$settings, $index) {
if (strpos($settings['layout'], 'field_') !== FALSE) {
$settings['layout'] = strip_tags($this
->getField($index, $settings['layout']));
}
}
public function getFieldRendered($index, $field_name = '', $restricted = FALSE) {
if (!empty($field_name) && ($output = $this
->getField($index, $field_name))) {
return is_array($output) ? $output : [
'#markup' => $restricted ? Xss::filterAdmin($output) : $output,
];
}
return [];
}
public function getFieldString($row, $field_name, $index) {
$values = [];
$renderer = $this->blazyManager
->getRenderer();
if ($value = $this
->getFieldValue($index, $field_name)) {
$value = is_array($value) ? array_filter($value) : $value;
if ($markup = $this
->getField($index, $field_name)) {
$value = is_object($markup) ? trim(strip_tags($markup
->__toString())) : $value;
}
if (is_string($value) && strpos($value, ',') !== FALSE) {
$tags = explode(',', $value);
$rendered_tags = [];
foreach ($tags as $tag) {
$rendered_tags[] = Html::cleanCssIdentifier(mb_strtolower(trim($tag)));
}
$values[$index] = implode(' ', $rendered_tags);
}
else {
$value = is_string($value) ? $value : (isset($value[0]['value']) && !empty($value[0]['value']) ? $value[0]['value'] : '');
$values[$index] = empty($value) ? '' : Html::cleanCssIdentifier(mb_strtolower($value));
}
}
if (empty($values)) {
if ($renderable = $this
->getFieldRenderable($row, $index, $field_name, TRUE)) {
$value = [];
foreach ($renderable as $key => $render) {
$class = isset($render['rendered']['#title']) ? $render['rendered']['#title'] : $renderer
->render($render['rendered']);
$class = trim(strip_tags($class));
$value[$key] = Html::cleanCssIdentifier(mb_strtolower($class));
}
$values[$index] = empty($value) ? '' : implode(' ', $value);
}
}
return $values;
}
}