View source
<?php
namespace Drupal\blazy;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityInterface;
class BlazyManager extends BlazyManagerBase {
public function cleanUpBreakpoints(array &$settings = []) {
if (!empty($settings['breakpoints'])) {
$breakpoints = array_filter(array_map('array_filter', $settings['breakpoints']));
$settings['breakpoints'] = NestedArray::filter($breakpoints, function ($breakpoint) {
return !(is_array($breakpoint) && (empty($breakpoint['width']) || empty($breakpoint['image_style'])));
});
}
}
public function isCrop($style = NULL) {
foreach ($style
->getEffects() as $effect) {
if (strpos($effect
->getPluginId(), 'crop') !== FALSE) {
return TRUE;
}
}
return FALSE;
}
public function setDimensionsOnce(array &$settings = []) {
$item = isset($settings['item']) ? $settings['item'] : NULL;
$dimensions['width'] = $settings['original_width'] = isset($item->width) ? $item->width : NULL;
$dimensions['height'] = $settings['original_height'] = isset($item->height) ? $item->height : NULL;
if (!empty($settings['image_style']) && ($style = $this
->entityLoad($settings['image_style']))) {
if ($this
->isCrop($style)) {
$style
->transformDimensions($dimensions, $settings['uri']);
$settings['height'] = $dimensions['height'];
$settings['width'] = $dimensions['width'];
$settings['_dimensions'] = TRUE;
}
}
if (!empty($settings['breakpoints'])) {
$this
->buildDataBlazy($settings, $item);
}
unset($settings['uri'], $settings['item']);
}
public function isBlazy(array &$settings, array $item = []) {
$content = !empty($settings['item_id']) && isset($item[$settings['item_id']]) ? $item[$settings['item_id']] : $item;
if (isset($item['settings'])) {
$blazy = is_array($content) && isset($content['#build']['settings']) ? $content['#build']['settings'] : [];
if (empty($settings['breakpoints']) && isset($blazy['breakpoints'])) {
$settings['breakpoints'] = $blazy['breakpoints'];
}
$cherries = [
'blazy',
'box_style',
'image_style',
'lazy',
'media_switch',
'ratio',
'uri',
];
foreach ($cherries as $key) {
$fallback = isset($settings[$key]) ? $settings[$key] : '';
$settings[$key] = isset($blazy[$key]) && empty($fallback) ? $blazy[$key] : $fallback;
}
}
if (is_array($content) && isset($content['#view']) && ($view = $content['#view'])) {
if ($blazy_field = BlazyViews::viewsField($view)) {
$settings = array_merge(array_filter($blazy_field
->mergedViewsSettings()), array_filter($settings));
}
}
$this
->cleanUpBreakpoints($settings);
if (!empty($settings['breakpoints'])) {
$image = isset($item['item']) ? $item['item'] : NULL;
$this
->buildDataBlazy($settings, $image);
}
unset($settings['uri']);
}
public function buildDataBlazy(array &$settings, $item = NULL) {
if (!empty($settings['blazy_data'])) {
return;
}
if (empty($settings['original_width'])) {
$settings['original_width'] = isset($item->width) ? $item->width : NULL;
$settings['original_height'] = isset($item->height) ? $item->height : NULL;
}
$json = $sources = [];
$end = end($settings['breakpoints']);
foreach ($settings['breakpoints'] as $key => $breakpoint) {
if (empty($breakpoint['image_style']) || empty($breakpoint['width'])) {
continue;
}
if ($width = Blazy::widthFromDescriptors($breakpoint['width'])) {
if (!empty($settings['uri']) && !empty($settings['ratio'])) {
$dimensions['width'] = $settings['original_width'];
$dimensions['height'] = $settings['original_height'];
if (!empty($breakpoint['image_style']) && ($style = $this
->entityLoad($breakpoint['image_style']))) {
if ($this
->isCrop($style)) {
$style
->transformDimensions($dimensions, $settings['uri']);
$padding = round($dimensions['height'] / $dimensions['width'] * 100, 2);
$json['dimensions'][$width] = $padding;
if ($end['width'] == $breakpoint['width']) {
$settings['padding_bottom'] = $padding;
}
}
}
}
if (!empty($settings['background'])) {
$sources[] = [
'width' => (int) $width,
'src' => 'data-src-' . $key,
];
}
}
}
if ($sources) {
$json['breakpoints'] = $sources;
}
if (isset($json['dimensions']) && count($settings['breakpoints']) != count($json['dimensions'])) {
unset($json['dimensions'], $settings['padding_bottom']);
}
if ($json) {
$settings['blazy_data'] = $json;
}
$settings['blazy'] = TRUE;
}
public function getBlazy(array $build = []) {
foreach (BlazyDefault::themeProperties() as $key) {
$build[$key] = isset($build[$key]) ? $build[$key] : [];
}
$settings =& $build['settings'];
$settings += BlazyDefault::itemSettings();
if (empty($build['content'])) {
$image = [
'#theme' => $settings['theme_hook_image'] ?: 'blazy',
'#delta' => $settings['delta'],
'#item' => $settings['entity_type_id'] == 'user' ? $build['item'] : [],
'#image_style' => $settings['image_style'],
'#build' => $build,
'#pre_render' => [
[
$this,
'preRenderImage',
],
],
];
}
else {
$image = $build['content'];
}
$this
->getModuleHandler()
->alter('blazy', $image, $settings);
return $image;
}
public function preRenderImage(array $element) {
$build = $element['#build'];
unset($element['#build']);
$item = $build['item'];
$settings = $build['settings'];
if (empty($settings['uri']) && is_object($item)) {
$settings['uri'] = ($entity = $item->entity) && empty($item->uri) ? $entity
->getFileUri() : $item->uri;
}
$item_attributes = [];
if ($item && isset($item->_attributes)) {
$item_attributes = $item->_attributes;
unset($item->_attributes);
}
if (!empty($settings['resimage'])) {
$settings['responsive_image_style_id'] = $settings['resimage']
->id();
$item_attributes['data-b-lazy'] = $settings['one_pixel'];
$settings['lazy'] = 'responsive';
$element['#cache']['tags'] = $this
->getResponsiveImageCacheTags($settings['resimage']);
}
else {
if (empty($settings['_no_cache'])) {
$file_tags = isset($settings['file_tags']) ? $settings['file_tags'] : [];
$settings['cache_tags'] = empty($settings['cache_tags']) ? $file_tags : Cache::mergeTags($settings['cache_tags'], $file_tags);
$element['#cache']['max-age'] = -1;
foreach ([
'contexts',
'keys',
'tags',
] as $key) {
if (!empty($settings['cache_' . $key])) {
$element['#cache'][$key] = $settings['cache_' . $key];
}
}
}
}
$element['#item'] = $item;
$element['#captions'] = empty($build['captions']) ? [] : [
'inline' => $build['captions'],
];
$element['#item_attributes'] = $item_attributes;
$element['#settings'] = $settings;
foreach ([
'caption',
'media',
'wrapper',
] as $key) {
if (!empty($settings[$key . '_attributes'])) {
$element["#{$key}" . '_attributes'] = $settings[$key . '_attributes'];
}
}
if (!empty($settings['media_switch'])) {
if ($settings['media_switch'] == 'content' && !empty($settings['content_url'])) {
$element['#url'] = $settings['content_url'];
}
elseif (!empty($settings['lightbox'])) {
BlazyLightbox::build($element);
}
}
return $element;
}
public function getEntityView($entity = NULL, array $settings = [], $fallback = '') {
if ($entity instanceof EntityInterface) {
$entity_type_id = $entity
->getEntityTypeId();
$view_hook = $entity_type_id . '_view';
$view_mode = empty($settings['view_mode']) ? 'default' : $settings['view_mode'];
$langcode = $entity
->language()
->getId();
if (function_exists($view_hook)) {
return $view_hook($entity, $view_mode, $langcode);
}
elseif ($this
->getEntityTypeManager()
->hasHandler($entity_type_id, 'view_builder')) {
return $this
->getEntityTypeManager()
->getViewBuilder($entity_type_id)
->view($entity, $view_mode, $langcode);
}
elseif ($fallback) {
return [
'#markup' => $fallback,
];
}
}
return FALSE;
}
public function getResponsiveImageCacheTags($responsive = NULL) {
$cache_tags = [];
$image_styles_to_load = [];
if ($responsive) {
$cache_tags = Cache::mergeTags($cache_tags, $responsive
->getCacheTags());
$image_styles_to_load = $responsive
->getImageStyleIds();
}
$image_styles = $this
->entityLoadMultiple('image_style', $image_styles_to_load);
foreach ($image_styles as $image_style) {
$cache_tags = Cache::mergeTags($cache_tags, $image_style
->getCacheTags());
}
return $cache_tags;
}
public function getImage(array $build = []) {
return $this
->getBlazy($build);
}
}