You are here

lazy.install in Lazy-load 8.3

Same filename and directory in other branches
  1. 8 lazy.install
  2. 8.2 lazy.install
  3. 7 lazy.install

Install, update, and uninstall functions for the Lazy-load module.

File

lazy.install
View source
<?php

/**
 * @file
 * Install, update, and uninstall functions for the Lazy-load module.
 */
use Drupal\Component\Serialization\Json;

/**
 * Implements hook_requirements().
 */
function lazy_requirements($phase) {
  if ($phase != 'runtime') {
    return [];
  }
  $settings = \Drupal::config('lazy.settings');
  $library_path = $settings
    ->get('libraryPath');
  $library_js = $settings
    ->get('minified') ? '/lazysizes.min.js' : '/lazysizes.js';
  $requirements['lazy_lazysizes'] = [
    'title' => t('lazySizes library'),
    'description' => t('<p>The lazySizes library needs to be installed at the <kbd>@path</kbd> folder in your Drupal installation directory.</p><p>If you <a href=":packagist">manage assets via Composer</a>, you can use <kbd>composer require bower-asset/lazysizes</kbd> to download the library.</p><p>If you <a href=":url">manually download the library</a>, make sure the folder name is <em>lazysizes</em>.</p>', [
      '@path' => $library_path,
      ':url' => 'https://github.com/aFarkas/lazysizes/releases/latest',
      ':packagist' => 'https://asset-packagist.org/',
    ]),
    'severity' => REQUIREMENT_ERROR,
  ];
  if (file_exists(DRUPAL_ROOT . $library_path . $library_js)) {
    $requirements['lazy_lazysizes']['severity'] = REQUIREMENT_OK;
    $requirements['lazy_lazysizes']['description'] = t('The lazySizes library is installed at <kbd>@path</kbd>', [
      '@path' => $library_path . $library_js,
    ]);
    if ($package_json = @file_get_contents(DRUPAL_ROOT . $library_path . '/package.json')) {
      $package_json = JSON::decode($package_json);
      if (isset($package_json['version'])) {
        $requirements['lazy_lazysizes']['value'] = $package_json['version'];
      }
    }
  }
  elseif (in_array(parse_url($library_path, PHP_URL_SCHEME), [
    'http',
    'https',
  ])) {
    $requirements['lazy_lazysizes']['severity'] = REQUIREMENT_OK;
    $requirements['lazy_lazysizes']['description'] = t('The lazySizes library is loaded from an external site, make sure it is a trusted source: <a href=":cdn" rel="noreferrer">:cdn</a>', [
      ':cdn' => $library_path . $library_js,
    ]);
  }
  return $requirements;
}

/**
 * Implements hook_install().
 */
function lazy_install() {
  lazy__update_migrate_config();
}

/**
 * Reset "disabled_paths" configuration to module default.
 */
function lazy_update_8201() {
  $config = \Drupal::service('config.factory')
    ->getEditable('lazy.settings');
  if ($config
    ->get('disabled_paths') === NULL) {
    $config
      ->set('disabled_paths', '/rss.xml');
    return t('The new "disabled_paths" configuration is set to "/rss.xml" (default value).');
  }
  return NULL;
}

/**
 * Reset "image_fields" configuration to module default.
 */
function lazy_update_8202() {
  $config = \Drupal::service('config.factory')
    ->getEditable('lazy.settings');
  if ($config
    ->get('image_fields') === TRUE) {
    $config
      ->set('image_fields', FALSE);
    return t('<b>Action needed:</b> As of 8.x-2.x, image fields are now controlled individually. You need to manually update each image field to enable lazy-loading.');
  }
  return NULL;
}

/**
 * Migrate lazy configuration.
 */
function lazy_update_8301() {
  lazy__update_migrate_config();
}

/**
 * Upgrade and migrate lazy configuration.
 */
function lazy__update_migrate_config() {
  $config = \Drupal::configFactory()
    ->getEditable('lazy.settings');

  // Add defaults for the new lazySizes library.
  if (empty($config
    ->get('lazysizes'))) {
    $lazysizes = [
      'lazyClass' => 'lazyload',
      'loadedClass' => 'lazyloaded',
      'loadingClass' => 'lazyloading',
      'preloadClass' => 'lazypreload',
      'errorClass' => 'lazyerror',
      'autosizesClass' => 'lazyautosizes',
      'srcAttr' => 'data-src',
      'srcsetAttr' => 'data-srcset',
      'sizesAttr' => 'data-sizes',
      'minSize' => 40,
      'customMedia' => [],
      'init' => TRUE,
      'expFactor' => 1.5,
      'hFac' => 0.8,
      'loadMode' => 2,
      'loadHidden' => TRUE,
      'ricTimeout' => 0,
      'throttleDelay' => 125,
      'plugins' => [],
    ];
    $config
      ->set('lazysizes', $lazysizes);
  }

  // Migrate legacy configuration.
  $selector = $config
    ->get('selector') ?: $config
    ->get('lazysizes.lazyClass');
  $errorClass = $config
    ->get('errorClass') ?: $config
    ->get('lazysizes.errorClass');
  $src = $config
    ->get('src') ?: $config
    ->get('lazysizes.srcAttr');
  $image_fields = (array) $config
    ->get('image_fields');
  $image_fields_updated = [];
  foreach ($image_fields as $image_field => $bool) {
    $parts = explode('--', $image_field);
    $key = implode('--', [
      $parts[0],
      $parts[1],
      $parts[3],
      $parts[2],
    ]);
    $image_fields_updated[$key] = TRUE;
  }
  $alter_tag = (array) $config
    ->get('alter_tag');
  foreach ($alter_tag as $key => $value) {
    $alter_tag[$key] = (string) $value;
  }
  $formatters = $config
    ->get('formatters') ?: [];
  foreach ($formatters as $key => $value) {
    $formatters[$key] = (string) $value;
  }
  $config
    ->set('lazysizes.lazyClass', $selector)
    ->set('lazysizes.errorClass', $errorClass)
    ->set('lazysizes.srcAttr', $src)
    ->set('image_fields', $image_fields_updated)
    ->set('alter_tag', $alter_tag)
    ->set('formatters', $formatters)
    ->clear('errorClass')
    ->clear('loadInvisible')
    ->clear('offset')
    ->clear('saveViewportOffsetDelay')
    ->clear('selector')
    ->clear('src')
    ->clear('successClass')
    ->clear('validateDelay')
    ->save(TRUE);
}

/**
 * Update lazy module configuration.
 */
function lazy_update_8302() {
  $config = \Drupal::configFactory()
    ->getEditable('lazy.settings');
  $config
    ->set('preferNative', TRUE)
    ->save();
}

/**
 * Update lazy module configuration for the new "preferNative" option.
 */
function lazy_update_8303() {
  $config = \Drupal::configFactory()
    ->getEditable('lazy.settings');
  $preferNative = (bool) ($config
    ->get('preferNative') !== NULL ? $config
    ->get('preferNative') : TRUE);
  $config
    ->set('preferNative', $preferNative)
    ->save(TRUE);
  return t('The new "preferNative" option is added, and set to %status.', [
    '%status' => $preferNative ? 'enabled' : 'disabled',
  ]);
}

/**
 * Remove "formatters" settings from the Lazy configuration.
 *
 * Now both "Image (Lazy-load)" and "Responsive image (Lazy-load)" image-
 * formatters are enabled at all times, as long as the core's "Image" and
 * "Responsive image" modules, respectively, are also enabled.
 */
function lazy_update_8304() {
  $config = \Drupal::configFactory()
    ->getEditable('lazy.settings');
  $config
    ->clear('formatters')
    ->save(TRUE);
}

/**
 * Set lazySizes library path default.
 */
function lazy_update_8305() {
  $config = \Drupal::configFactory()
    ->getEditable('lazy.settings');
  $config
    ->set('libraryPath', '/libraries/lazysizes')
    ->save(TRUE);
}

/**
 * Update all lazy-loading enabled text-formats with carried-over settings.
 */
function lazy_update_8306() {
  $configFactory = \Drupal::configFactory();
  $migration_settings = [];
  $config = $configFactory
    ->getEditable('lazy.settings');
  $alter_tag = $config
    ->get('alter_tag');
  foreach ($alter_tag as $tag => $status) {
    $key = $tag === 'img' ? 'image' : $tag;
    $migration_settings[$key] = (bool) $alter_tag[$tag];
  }
  foreach (filter_formats() as $key => $filter) {
    $filter_configuration = $filter
      ->filters()
      ->getConfiguration();
    if (isset($filter_configuration['lazy_filter'])) {
      $format_settings = $configFactory
        ->getEditable("filter.format.{$key}");
      $format_settings
        ->set('filters.lazy_filter.settings', $migration_settings)
        ->save(TRUE);
    }
  }
  $config
    ->clear('image_fields')
    ->clear('alter_tag')
    ->save(TRUE);
}

/**
 * Enable new configuration with default value `minified = TRUE`.
 */
function lazy_update_8307() {
  $config = \Drupal::configFactory()
    ->getEditable('lazy.settings');
  $config
    ->set('minified', TRUE)
    ->save(TRUE);
}

/**
 * Migrate "disabled paths" into new "visibility" setting.
 */
function lazy_update_8308() {
  $config = \Drupal::configFactory()
    ->getEditable('lazy.settings');
  $disabled_paths = $config
    ->get('disabled_paths');
  $config
    ->set('disable_admin', TRUE)
    ->set('visibility', [
    'id' => 'request_path',
    'pages' => $disabled_paths,
    'negate' => 0,
  ])
    ->clear('disabled_paths')
    ->save(TRUE);
}

Functions

Namesort descending Description
lazy_install Implements hook_install().
lazy_requirements Implements hook_requirements().
lazy_update_8201 Reset "disabled_paths" configuration to module default.
lazy_update_8202 Reset "image_fields" configuration to module default.
lazy_update_8301 Migrate lazy configuration.
lazy_update_8302 Update lazy module configuration.
lazy_update_8303 Update lazy module configuration for the new "preferNative" option.
lazy_update_8304 Remove "formatters" settings from the Lazy configuration.
lazy_update_8305 Set lazySizes library path default.
lazy_update_8306 Update all lazy-loading enabled text-formats with carried-over settings.
lazy_update_8307 Enable new configuration with default value `minified = TRUE`.
lazy_update_8308 Migrate "disabled paths" into new "visibility" setting.
lazy__update_migrate_config Upgrade and migrate lazy configuration.