You are here

function lazy__update_migrate_config in Lazy-load 8.3

Upgrade and migrate lazy configuration.

2 calls to lazy__update_migrate_config()
lazy_install in ./lazy.install
Implements hook_install().
lazy_update_8301 in ./lazy.install
Migrate lazy configuration.

File

./lazy.install, line 99
Install, update, and uninstall functions for the Lazy-load module.

Code

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);
}