You are here

function leaflet_requirements in Leaflet 8

Same name and namespace in other branches
  1. 7 leaflet.install \leaflet_requirements()
  2. 2.1.x leaflet.install \leaflet_requirements()
  3. 2.0.x leaflet.install \leaflet_requirements()

Implements hook_requirements().

File

./leaflet.install, line 14
Contains the leaflet.install file.

Code

function leaflet_requirements($phase) {
  $requirements = [];
  if ($phase != 'runtime') {
    return $requirements;
  }
  $library = \Drupal::service('library.discovery')
    ->getLibraryByName('leaflet', 'leaflet');
  $requirements['leaflet'] = [
    'title' => \Drupal::translation()
      ->translate('Leaflet library'),
  ];
  $maps_info = \Drupal::translation()
    ->translate('@maps available.', [
    '@maps' => \Drupal::translation()
      ->formatPlural(count(leaflet_map_get_info()), 'One map', '@count maps'),
  ]);

  // Check the defined type of the leaflet.js file; if it is external then
  // assume that we are using a CDN version.
  if ($library['js'][0]['type'] == 'external') {
    $requirements['leaflet']['value'] = \Drupal::translation()
      ->translate('Using CDN version @version.', [
      '@version' => $library['version'],
    ]) . ' ' . $maps_info;
  }
  else {
    if (file_exists($library['js'][0]['data'])) {
      $requirements['leaflet']['value'] = \Drupal::translation()
        ->translate('Leaflet @version library installed at @path.', [
        '@version' => $library['version'],
        '@path' => $library['js'][0]['data'],
      ]) . ' ' . $maps_info;
      $requirements['leaflet']['severity'] = REQUIREMENT_OK;
    }
    else {
      $requirements['leaflet']['value'] = \Drupal::translation()
        ->translate('Leaflet @version library not found at @path. Please !download it to @directory, or undo your changes to the libraries registry to use the CDN version.', [
        '@version' => $library['version'],
        '@path' => $library['js'][0]['data'],
        '@directory' => dirname($library['js'][0]['data']),
        '!download' => Link::fromTextAndUrl('download', Url::fromUri($library['remote'])),
      ]);
      $requirements['leaflet']['severity'] = REQUIREMENT_ERROR;
    }
  }
  return $requirements;
}