function lazy_page_attachments in Lazy-load 8.3
Same name and namespace in other branches
- 8 lazy.module \lazy_page_attachments()
- 8.2 lazy.module \lazy_page_attachments()
Implements hook_page_attachments().
File
- ./
lazy.module, line 275 - Module file for Lazy-load.
Code
function lazy_page_attachments(array &$attachments) {
$lazy_service = \Drupal::service('lazy');
$config = $lazy_service
->getSettings();
if ($config && $lazy_service
->isPathAllowed()) {
$plugins = $lazy_service
->getPlugins();
$config['lazysizes']['plugins'] = array_intersect_key($plugins, $config['lazysizes']['plugins']);
$options = [
'lazysizes' => $config['lazysizes'],
'placeholderSrc' => $config['placeholderSrc'],
'preferNative' => $config['preferNative'],
'minified' => $config['minified'] ?? TRUE,
'libraryPath' => $config['libraryPath'] ?? '/libraries/lazysizes',
];
$attachments['#attached']['drupalSettings']['lazy'] = $options;
$attachments['#attached']['library'][] = 'lazy/lazy';
$lazyloaded = $config['lazysizes']['loadedClass'];
$lazyloading = $config['lazysizes']['loadingClass'];
$lazyclass = $config['lazysizes']['lazyClass'];
$sizes = $config['lazysizes']['sizesAttr'];
$cssEffect = '';
if ((bool) ($config['cssEffect'] ?? FALSE)) {
$cssEffect = '/* Transition effect. */' . ".js .{$lazyclass}, .js .{${$lazyloading}} { opacity: 0; }" . ".js .{$lazyloaded} { opacity: 1; -webkit-transition: opacity 2000ms; transition: opacity 2000ms; }";
}
$attachments['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'style',
'#value' => '/* @see https://github.com/aFarkas/lazysizes#broken-image-symbol */' . ".js img.{$lazyclass}:not([src]) { visibility: hidden; }" . '/* @see https://github.com/aFarkas/lazysizes#automatically-setting-the-sizes-attribute */' . ".js img.{$lazyloaded}[{$sizes}=auto] { display: block; width: 100%; }" . $cssEffect,
],
'lazy-lazysizes',
];
$attachments['#cache']['tags'][] = 'config:lazy.settings';
}
}