View source
<?php
namespace Drupal\blazy;
use Drupal\Core\Template\Attribute;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\Component\Serialization\Json;
use Drupal\image\Entity\ImageStyle;
class Blazy implements BlazyInterface {
const PLACEHOLDER = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
private static $blazyId;
public static function buildAttributes(&$variables) {
$element = $variables['element'];
foreach (BlazyDefault::themeProperties() as $key) {
$variables[$key] = isset($element["#{$key}"]) ? $element["#{$key}"] : [];
}
foreach (BlazyDefault::themeAttributes() as $key) {
$key = $key . '_attributes';
$variables[$key] = empty($element["#{$key}"]) ? [] : new Attribute($element["#{$key}"]);
}
$settings =& $variables['settings'];
$settings += BlazyDefault::itemSettings();
$settings['ratio'] = empty($settings['ratio']) ? '' : str_replace(':', '', $settings['ratio']);
$settings['use_media'] = $settings['embed_url'] && in_array($settings['type'], [
'audio',
'video',
]);
self::buildUrl($settings, $variables['item']);
if (empty($settings['uri'])) {
return;
}
$settings['extension'] = pathinfo($settings['uri'])['extension'];
if (!empty($settings['responsive_image_style_id']) && $settings['extension'] != 'svg') {
self::buildResponsiveImage($variables);
}
else {
self::buildImage($variables);
}
if ($settings['use_media'] && empty($settings['_noiframe'])) {
self::buildIframeAttributes($variables);
}
if ($variables['image']) {
self::imageAttributes($variables);
}
self::thumbnailAttributes($variables);
}
public static function buildResponsiveImage(array &$variables) {
$image =& $variables['image'];
$settings =& $variables['settings'];
$image['#type'] = 'responsive_image';
$image['#responsive_image_style_id'] = $settings['responsive_image_style_id'];
$image['#uri'] = $settings['uri'];
$settings['ratio'] = FALSE;
}
public static function buildImage(array &$variables) {
$image =& $variables['image'];
$settings =& $variables['settings'];
$attributes =& $variables['attributes'];
$image_attributes =& $variables['item_attributes'];
$image['#theme'] = 'image';
$image['#uri'] = empty($settings['image_url']) ? $settings['uri'] : $settings['image_url'];
if (!empty($settings['width'])) {
if (!empty($settings['ratio']) && in_array($settings['ratio'], [
'enforced',
'fluid',
])) {
$padding_bottom = empty($settings['padding_bottom']) ? round($settings['height'] / $settings['width'] * 100, 2) : $settings['padding_bottom'];
$attributes['style'] = 'padding-bottom: ' . $padding_bottom . '%';
$settings['_breakpoint_ratio'] = $settings['ratio'];
}
}
if (!empty($settings['lazy'])) {
$image['#uri'] = static::PLACEHOLDER;
if (empty($settings['background']) || empty($settings['blazy'])) {
self::buildBreakpointAttributes($image_attributes, $settings);
}
if (!empty($settings['background'])) {
self::buildBreakpointAttributes($attributes, $settings);
$attributes['class'][] = 'media--background';
if (!empty($settings['blazy'])) {
$image = [];
}
}
if (!empty($settings['blazy_data']['dimensions'])) {
$attributes['data-dimensions'] = Json::encode($settings['blazy_data']['dimensions']);
}
}
}
public static function containerAttributes(array &$attributes, array $settings = []) {
$classes = empty($attributes['class']) ? [] : $attributes['class'];
$attributes['data-blazy'] = empty($settings['blazy_data']) ? '' : Json::encode($settings['blazy_data']);
if (!empty($settings['media_switch'])) {
$switch = str_replace('_', '-', $settings['media_switch']);
$attributes['data-' . $switch . '-gallery'] = TRUE;
}
if (isset($settings['namespace']) && $settings['namespace'] == 'blazy') {
foreach ([
'field',
'view',
] as $key) {
if (!empty($settings[$key . '_name'])) {
$classes[] = 'blazy--' . $key . ' blazy--' . str_replace('_', '-', $settings[$key . '_name']);
}
}
}
$attributes['class'] = array_merge([
'blazy',
], $classes);
}
public static function imageAttributes(array &$variables) {
$item = $variables['item'];
$image =& $variables['image'];
$settings = $variables['settings'];
$attributes =& $variables['item_attributes'];
if ($item) {
if (!isset($attributes['alt'])) {
$attributes['alt'] = isset($item->alt) ? $item->alt : NULL;
}
if (isset($item->title) && mb_strlen($item->title) != 0) {
$attributes['title'] = $item->title;
}
}
if (!isset($attributes['width']) && $settings['extension'] != 'svg') {
$image['#width'] = $settings['width'];
$image['#height'] = $settings['height'];
}
$attributes['class'][] = 'media__image media__element';
$image['#attributes'] = $attributes;
}
public static function thumbnailAttributes(array &$variables) {
$settings = $variables['settings'];
$attributes =& $variables['attributes'];
if (!empty($settings['thumbnail_uri'])) {
$attributes['data-thumb'] = file_url_transform_relative(file_create_url($settings['thumbnail_uri']));
}
elseif (!empty($settings['thumbnail_style'])) {
$attributes['data-thumb'] = ImageStyle::load($settings['thumbnail_style'])
->buildUrl($settings['uri']);
}
}
public static function buildIframeAttributes(&$variables) {
$settings =& $variables['settings'];
$variables['image'] = empty($settings['media_switch']) ? [] : $variables['image'];
$settings['player'] = empty($settings['lightbox']) && $settings['media_switch'] != 'content';
$iframe['data-src'] = $settings['embed_url'];
$iframe['src'] = empty($settings['iframe_lazy']) ? $settings['embed_url'] : 'about:blank';
if (!empty($settings['iframe_lazy']) && empty($settings['media_switch'])) {
$iframe['class'][] = 'b-lazy';
}
if (empty($settings['ratio']) && !empty($settings['width'])) {
$iframe['width'] = $settings['width'];
$iframe['height'] = $settings['height'];
}
$settings['autoplay_url'] = empty($settings['autoplay_url']) ? $settings['embed_url'] : $settings['autoplay_url'];
$variables['iframe_attributes'] = new Attribute($iframe);
$variables['attributes']['data-media'] = Json::encode([
'type' => $settings['type'],
'scheme' => $settings['scheme'],
]);
}
public static function buildBreakpointAttributes(array &$attributes = [], array &$settings = []) {
$attributes['class'][] = $settings['lazy_class'];
$attributes['data-' . $settings['lazy_attribute']] = $settings['image_url'];
if (empty($settings['breakpoints'])) {
return;
}
$srcset = $json = [];
foreach ($settings['breakpoints'] as $key => $breakpoint) {
if (empty($breakpoint['image_style']) || empty($breakpoint['width'])) {
continue;
}
if ($style = ImageStyle::load($breakpoint['image_style'])) {
$url = file_url_transform_relative($style
->buildUrl($settings['uri']));
if (!empty($settings['_breakpoint_ratio']) && empty($settings['blazy_data']['dimensions'])) {
$dimensions = [
'width' => $settings['width'],
'height' => $settings['height'],
];
$style
->transformDimensions($dimensions, $settings['uri']);
if ($width = self::widthFromDescriptors($breakpoint['width'])) {
$json[$width] = round($dimensions['height'] / $dimensions['width'] * 100, 2);
}
}
$settings['breakpoints'][$key]['url'] = $url;
if (!empty($settings['background'])) {
$attributes['data-src-' . $key] = $url;
}
elseif (!empty($breakpoint['width'])) {
$width = trim($breakpoint['width']);
$width = is_numeric($width) ? $width . 'w' : $width;
$srcset[] = $url . ' ' . $width;
}
}
}
if ($srcset) {
$settings['srcset'] = implode(', ', $srcset);
$attributes['srcset'] = '';
$attributes['data-srcset'] = $settings['srcset'];
$attributes['sizes'] = '100w';
if (!empty($settings['sizes'])) {
$attributes['sizes'] = trim($settings['sizes']);
unset($attributes['height'], $attributes['width']);
}
}
if ($json) {
$settings['blazy_data']['dimensions'] = $json;
}
}
public static function buildUrl(array &$settings = [], $item = NULL) {
if (empty($settings['uri']) && $item) {
$settings['uri'] = ($entity = $item->entity) && empty($item->uri) ? $entity
->getFileUri() : $item->uri;
}
if (empty($settings['uri'])) {
return;
}
if (empty($settings['image_url'])) {
$settings['image_url'] = file_url_transform_relative(file_create_url($settings['uri']));
}
if (empty($settings['width'])) {
$settings['width'] = $item && isset($item->width) ? $item->width : NULL;
$settings['height'] = $item && isset($item->height) ? $item->height : NULL;
}
if (!empty($settings['image_style']) && ($style = ImageStyle::load($settings['image_style']))) {
$settings['image_url'] = file_url_transform_relative($style
->buildUrl($settings['uri']));
$settings['cache_tags'] = $style
->getCacheTags();
if (empty($settings['_dimensions'])) {
$style
->transformDimensions($settings, $settings['uri']);
}
}
}
public static function widthFromDescriptors($descriptor = '') {
$descriptor = trim($descriptor);
if (is_numeric($descriptor)) {
return (int) $descriptor;
}
$width = strpos($descriptor, "w") !== FALSE ? str_replace('w', '', $descriptor) : $descriptor;
if (strpos($descriptor, " ") !== FALSE) {
list($width, $px) = array_pad(array_map('trim', explode(" ", $width, 2)), 2, NULL);
if (is_numeric($px) && strpos($width, "x") !== FALSE) {
$width = $px;
}
}
return is_numeric($width) ? (int) $width : FALSE;
}
public static function preprocessResponsiveImage(&$variables) {
$image =& $variables['img_element'];
$attributes =& $variables['attributes'];
if (!$variables['output_image_tag']) {
if (isset($variables['sources']) && is_array($variables['sources'])) {
foreach ($variables['sources'] as &$source) {
$source
->setAttribute('data-srcset', $source['srcset']
->value());
$source
->removeAttribute('srcset');
}
}
$fallback_uri = isset($image['#srcset'], $image['#srcset'][0]['uri']) ? $image['#srcset'][0]['uri'] : $image['#uri'];
$image['#uri'] = static::PLACEHOLDER;
$image['#srcset'] = '';
unset($attributes['data-srcset'], $image['#attributes']['data-srcset']);
}
else {
$fallback_uri = $image['#uri'];
$attributes['data-srcset'] = $attributes['srcset']
->value();
$image['#attributes']['data-srcset'] = $attributes['srcset']
->value();
$image['#attributes']['srcset'] = '';
}
$image['#attributes']['data-src'] = $fallback_uri;
$image['#attributes']['class'][] = 'b-lazy b-responsive';
if (!empty($attributes['data-b-lazy'])) {
$image['#uri'] = static::PLACEHOLDER;
}
unset($attributes['data-b-lazy'], $image['#attributes']['data-b-lazy']);
}
public static function configSchemaInfoAlter(array &$definitions, $formatter = 'blazy_base', $settings = []) {
if (isset($definitions[$formatter])) {
$mappings =& $definitions[$formatter]['mapping'];
$settings = $settings ?: BlazyDefault::extendedSettings() + BlazyDefault::gridSettings();
foreach ($settings as $key => $value) {
$type = gettype($value);
$type = $type == 'double' ? 'float' : $type;
$mappings[$key]['type'] = $key == 'breakpoints' ? 'mapping' : (is_array($value) ? 'sequence' : $type);
if (!is_array($value)) {
$mappings[$key]['label'] = Unicode::ucfirst(str_replace('_', ' ', $key));
}
}
if (isset($mappings['breakpoints'])) {
foreach (BlazyDefault::getConstantBreakpoints() as $breakpoint) {
$mappings['breakpoints']['mapping'][$breakpoint]['type'] = 'mapping';
foreach ([
'breakpoint',
'width',
'image_style',
] as $item) {
$mappings['breakpoints']['mapping'][$breakpoint]['mapping'][$item]['type'] = 'string';
$mappings['breakpoints']['mapping'][$breakpoint]['mapping'][$item]['label'] = Unicode::ucfirst(str_replace('_', ' ', $item));
}
}
}
foreach ([
'dimension',
'display',
'item_id',
] as $key) {
$mappings[$key]['type'] = 'string';
}
}
}
public static function getHtmlId($string = 'blazy', $id = '') {
if (!isset(static::$blazyId)) {
static::$blazyId = 0;
}
return empty($id) ? Html::getId($string . '-' . ++static::$blazyId) : strip_tags($id);
}
public static function getConfig($setting_name = '', $settings = 'blazy.settings') {
return \Drupal::service('blazy.manager')
->configLoad($setting_name, $settings);
}
public static function isCrop($style = NULL) {
return \Drupal::service('blazy.manager')
->isCrop($style);
}
}