Skip to content

Instantly share code, notes, and snippets.

@laradevitt
Created May 1, 2021 00:50
Show Gist options
  • Save laradevitt/64f0a6c2c353a19ce921d90d9f3ebcbe to your computer and use it in GitHub Desktop.
Save laradevitt/64f0a6c2c353a19ce921d90d9f3ebcbe to your computer and use it in GitHub Desktop.
(Drupal 8) Resolves 'missing required attribute' validation errors when using Lazy-load (lazy) module. Versions: lazy 3.6.0, lazysizes 5.3.1
<?php
/**
* Implements hook_preprocess_image().
*
* Adding src here fixes validation error 'Element img is missing required
* attribute src' when using the lazysizes library.
* https://github.com/aFarkas/lazysizes/issues/500#issuecomment-616115829
*/
function MYTHEME_preprocess_image(array &$variables) {
if (!isset($variables['attributes']['src'])) {
$variables['attributes']['src'] = 'data:,1w';
}
}
/**
* Implements hook_preprocess_responsive_image().
*
* Adding srcset here fixes validation error 'Element source is missing required
* attribute srcset' when using the lazysizes library.
* https://github.com/aFarkas/lazysizes/issues/500#issuecomment-616115829
*/
function MYTHEME_preprocess_responsive_image(array &$variables) {
foreach ($variables['sources'] as $k => $source_attributes) {
if (!isset($source_attributes['srcset'])) {
$variables['sources'][$k]['srcset'] = 'data:,1w';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment