You are here

function lazy_requirements in Lazy-load 8.3

Same name and namespace in other branches
  1. 8 lazy.install \lazy_requirements()
  2. 8.2 lazy.install \lazy_requirements()
  3. 7 lazy.install \lazy_requirements()

Implements hook_requirements().

File

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

Code

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