You are here

function image_lazy_loader_preprocess_image in Image Lazy Loader 8

Implements hook_preprocess_HOOK().

Swap the 'srcset' attribute for the 'data-srcset' that Lozad expets. Add a class to the image tag, too.

File

./image_lazy_loader.module, line 131
Add Lozad lazy-loading to image / responsive image fields.

Code

function image_lazy_loader_preprocess_image(&$variables) {
  $is_lazy_loaded = FALSE;
  if (isset($variables['attributes'], $variables['attributes']['lazy'])) {
    $is_lazy_loaded = $variables['attributes']['lazy'];
  }
  if ($is_lazy_loaded) {
    if (isset($variables['attributes']['srcset'])) {
      $variables['attributes']['data-srcset'] = $variables['attributes']['srcset'];
      unset($variables['attributes']['srcset']);
    }
    else {
      $variables['attributes']['data-src'] = $variables['attributes']['src'];
    }
    $variables['attributes']['src'] = '';
    $variables['attributes']['class'][] = 'lozad';
    $variables['lazy'] = TRUE;
  }
  unset($variables['attributes']['lazy']);
}