You are here

lazyloader.module in Image Lazyloader 8

Same filename and directory in other branches
  1. 7.2 lazyloader.module
  2. 7 lazyloader.module

Lazyloader Module.

Note: Obviously, this will not work unless javascript is enabled.

File

lazyloader.module
View source
<?php

/**
 * @file
 * Lazyloader Module.
 *
 * Note: Obviously, this will not work unless javascript is enabled.
 */
use Drupal\Core\Template\Attribute;
use Drupal\lazyloader\Hooks\ThemeRegistryAlter;
use Drupal\lazyloader\ResponsiveImage;

/**
 * Allow modules to modify assets prior to modification or skip it altogether.
 *
 * @param array $vars
 *   The preprocess_[responsive_]image() arguments.
 *
 * @return bool
 *   Should lazyloader ignore this asset ?
 */
function _lazyloader_alter(array &$vars) {

  // Allow alter for modules.
  \Drupal::moduleHandler()
    ->alter('lazyloader', $vars);
  if (!empty($vars['lazyloader_ignore'])) {
    return TRUE;
  }

  // Allow alter for themes.
  \Drupal::theme()
    ->alter('lazyloader', $vars);
  if (!empty($vars['lazyloader_ignore'])) {
    return TRUE;
  }
  unset($vars['lazyloader_ignore']);
  return FALSE;
}

/**
 * Implements hook_theme_registry_alter().
 */
function lazyloader_theme_registry_alter(&$theme_registry) {
  $alter = new ThemeRegistryAlter(\Drupal::moduleHandler(), \Drupal::configFactory());
  $alter
    ->themeRegistryAlter($theme_registry);
}

/**
 * Implements hook_preprocess_image().
 */
function lazyloader_preprocess_image(&$vars) {
  if (_lazyloader_alter($vars)) {
    return;
  }

  /** @var \Drupal\lazyloader\ThemePreprocess $preprocess */
  $preprocess = \Drupal::service('lazyloader.preprocess');
  $vars = $preprocess
    ->addCacheTags($vars);
  $vars = $preprocess
    ->attachLibrary($vars);
  $config = \Drupal::config('lazyloader.configuration');

  // Add noscript as a fallback.
  $vars['old_attributes'] = new Attribute($vars['attributes']);

  // Add support for responsive images.
  // @todo Ensure that this logic is enough.
  if (!empty($vars['attributes']['srcset'])) {
    $responsive_image = ResponsiveImage::parse($vars['attributes']['srcset']);
    $uri = $responsive_image
      ->get(0)->uri;
  }
  else {
    $uri = $vars['uri'];
  }
  unset($vars['attributes']['srcset']);
  if ($vars['theme_hook_original'] !== 'lazyloader_image') {

    /** @var \Drupal\lazyloader\VisibilityChecker $checker */
    $checker = \Drupal::service('lazyloader.visibility_checker');
    if (!$checker
      ->isEnabled() || !$checker
      ->isValidFilename($uri) || !$checker
      ->isValidImageStyle($uri)) {
      return;
    }
  }
  $image_placeholder_src = NULL;

  // Set placeholder image.
  if (!isset($image_placeholder_src)) {

    // Path to dummy placeholder image, to be replaced by actual image.
    $image_placeholder = trim($config
      ->get('placeholder'));
    $image_placeholder_src = $image_placeholder ? base_path() . $image_placeholder : 'data:image/gif;base64,R0lGODlhAQABAIAAAP7//wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==';
  }
  if (isset($vars['attributes']['class']) && is_string($vars['attributes']['class'])) {
    $vars['attributes']['class'] = explode(" ", $vars['attributes']['class']);
  }
  $vars['attributes']['class'][] = 'lazyload';
  $vars['attributes']['src'] = file_create_url($image_placeholder_src);
  if (isset($responsive_image)) {
    $vars['attributes']['data-srcset'] = $responsive_image
      ->__toString();
  }
  else {
    $vars['attributes']['data-src'] = file_create_url($uri);
  }
}

/**
 * Implements hook_preprocess_responsive_image().
 */
function lazyloader_preprocess_responsive_image(&$vars) {
  if (_lazyloader_alter($vars)) {
    return;
  }

  /** @var \Drupal\lazyloader\ThemePreprocess $preprocess */
  $preprocess = \Drupal::service('lazyloader.preprocess');
  $vars = $preprocess
    ->attachLibrary($vars);
  $vars = $preprocess
    ->addCacheTags($vars);

  /** @var \Drupal\lazyloader\VisibilityChecker $checker */
  $checker = \Drupal::service('lazyloader.visibility_checker');
  if (!$checker
    ->isEnabled()) {
    return;
  }

  /** @var \Drupal\Core\Template\Attribute $image_attributes */
  foreach ($vars['sources'] as $key => $image_attributes) {
    $responsive_image = ResponsiveImage::parse((string) $image_attributes['srcset']);
    $uri = $responsive_image
      ->get(0)->uri;
    if (!$checker
      ->isValidFilename($uri) || !$checker
      ->isValidImageStyle($uri)) {
      continue;
    }
    $image_attributes
      ->addClass('lazyload');
    unset($vars['sources'][$key]['srcset']);
    $vars['sources'][$key]['data-srcset'] = $responsive_image
      ->__toString();
  }
}

/**
 * Implements hook_libraries_info().
 */
function lazyloader_libraries_info() {
  $libraries['lazysizes'] = [
    'name' => 'Lazysizes',
    'vendor url' => 'https://afarkas.github.io/lazysizes',
    'download url' => 'https://github.com/aFarkas/lazysizes/archive/master.zip',
    'path' => 'dist',
    'files' => [
      'js' => [
        'lazysizes.js',
      ],
    ],
    'variants' => [
      'minified' => [
        'files' => [
          'js' => [
            'lazysizes.min.js',
          ],
        ],
      ],
    ],
  ];
  return $libraries;
}

Functions

Namesort descending Description
lazyloader_libraries_info Implements hook_libraries_info().
lazyloader_preprocess_image Implements hook_preprocess_image().
lazyloader_preprocess_responsive_image Implements hook_preprocess_responsive_image().
lazyloader_theme_registry_alter Implements hook_theme_registry_alter().
_lazyloader_alter Allow modules to modify assets prior to modification or skip it altogether.