public static function Blazy::preprocessResponsiveImage in Blazy 8.2
Same name and namespace in other branches
- 8 src/Blazy.php \Drupal\blazy\Blazy::preprocessResponsiveImage()
Overrides variables for responsive-image.html.twig templates.
2 calls to Blazy::preprocessResponsiveImage()
- BlazyManagerTest::testPreprocessResponsiveImage in tests/
src/ Kernel/ BlazyManagerTest.php - Tests responsive image integration.
- blazy_preprocess_responsive_image in ./
blazy.module - Overrides variables for responsive-image.html.twig templates.
File
- src/
Blazy.php, line 331
Class
- Blazy
- Provides common blazy utility static methods.
Namespace
Drupal\blazyCode
public static function preprocessResponsiveImage(array &$variables) {
$image =& $variables['img_element'];
$attributes =& $variables['attributes'];
$placeholder = empty($attributes['data-placeholder']) ? static::PLACEHOLDER : $attributes['data-placeholder'];
// Bail out if a noscript is requested.
// @todo figure out to not even enter this method, yet not break ratio, etc.
if (!isset($attributes['data-b-noscript'])) {
// Modifies <picture> [data-srcset] attributes on <source> elements.
if (!$variables['output_image_tag']) {
/** @var \Drupal\Core\Template\Attribute $source */
if (isset($variables['sources']) && is_array($variables['sources'])) {
foreach ($variables['sources'] as &$source) {
$source
->setAttribute('data-srcset', $source['srcset']
->value());
$source
->setAttribute('srcset', '');
}
}
// Prevents invalid IMG tag when one pixel placeholder is disabled.
$image['#uri'] = $placeholder;
$image['#srcset'] = '';
// Cleans up the no-longer relevant attributes for controlling element.
unset($attributes['data-srcset'], $image['#attributes']['data-srcset']);
}
else {
// Modifies <img> element attributes.
$image['#attributes']['data-srcset'] = $attributes['srcset']
->value();
$image['#attributes']['srcset'] = '';
}
// The [data-b-lazy] is a flag indicating 1px placeholder.
// This prevents double-downloading the fallback image, if enabled.
if (!empty($attributes['data-b-lazy'])) {
$image['#uri'] = $placeholder;
}
// More shared-with-image attributes are set at self::imageAttributes().
$image['#attributes']['class'][] = 'b-responsive';
}
// Cleans up the no-longer needed flags:
foreach ([
'placeholder',
'b-lazy',
'b-noscript',
] as $key) {
unset($attributes['data-' . $key], $image['#attributes']['data-' . $key]);
}
}