You are here

function lazy_process_variables in Lazy-load 8.3

Process the variables.

Parameters

array $variables: Variables array.

1 call to lazy_process_variables()
lazy_preprocess_image in ./lazy.module
Implements template_preprocess_image().

File

./lazy.module, line 175
Module file for Lazy-load.

Code

function lazy_process_variables(array &$variables) {
  $lazy_service = \Drupal::service('lazy');
  if ($lazy_service
    ->isEnabled($variables['attributes'])) {
    $config = $lazy_service
      ->getSettings();
    if ($config['preferNative']) {

      // Required attribute for enabling native lazy-loading.
      $variables['attributes']['loading'] = 'lazy';
    }
    else {

      // Set the selector class.
      $variables['attributes']['class'][] = $config['lazysizes']['lazyClass'];

      // Set the new `src` attribute.
      $variables['attributes'][$config['lazysizes']['srcAttr']] = $variables['attributes']['src'];

      // Placeholder variables.
      $lazy = $variables['attributes']['data-lazy'] ?? [];
      $image_file_id = $lazy['fid'] ?? 0;
      $placeholder_style = $lazy['placeholder_style'] ?? FALSE;
      $placeholder_data_uri = $lazy['data_uri'] ?? FALSE;
      if ($placeholder_style && $image_file_id > 0) {

        /** @var \Drupal\file\Entity\File $file */
        $file = File::load($image_file_id);

        /** @var \Drupal\image\Entity\ImageStyle $style */
        $style = ImageStyle::load($placeholder_style);

        // Build the derivative image URL.
        $placeholder_image_url = $style
          ->buildUrl($file
          ->getFileUri());

        // Serve as a data URI?
        if ($placeholder_data_uri) {

          // Requesting the URL will cause the image to be created.
          $content = file_get_contents($placeholder_image_url);

          // Get the mime-type of the styled image, falls back to original.
          $mime_type = function_exists('mime_content_type') ? mime_content_type($style
            ->buildUri($file
            ->getFileUri())) : $file
            ->getMimeType();

          // Build the data URI with matching mime-type.
          $placeholder_image_url = "data:{$mime_type};base64," . base64_encode($content);
        }
        else {
          $placeholder_image_url = file_url_transform_relative($placeholder_image_url);
        }
        $variables['attributes']['src'] = $placeholder_image_url;
      }
      elseif ($config['placeholderSrc']) {
        $variables['attributes']['src'] = $config['placeholderSrc'];
      }
      else {
        unset($variables['attributes']['src']);
      }

      // Set the new `srcset` attribute.
      if (isset($variables['attributes']['srcset'])) {
        $variables['attributes'][$config['lazysizes']['srcsetAttr']] = $variables['attributes']['srcset'];
        unset($variables['attributes']['srcset']);
      }

      // Set the new `sizes` attribute.
      if (isset($variables['attributes']['sizes'])) {
        $variables['attributes'][$config['lazysizes']['sizesAttr']] = $variables['attributes']['sizes'];
        unset($variables['attributes']['sizes']);
      }
    }
  }
}