You are here

function image_widget_crop_library_info_alter in Image Widget Crop 8.2

Same name and namespace in other branches
  1. 8 image_widget_crop.module \image_widget_crop_library_info_alter()

Implements hook_library_info_alter().

File

./image_widget_crop.module, line 69
Contains image_widget_crop.module.

Code

function image_widget_crop_library_info_alter(&$libraries, $extension) {
  if ($extension != 'image_widget_crop') {
    return;
  }
  $config = \Drupal::config('image_widget_crop.settings');
  $js = $config
    ->get('settings.library_url');
  $css = $config
    ->get('settings.css_url');

  // Explicit configuration takes priority.
  if (!empty($js) && !empty($css)) {
    $files = [
      'js' => $js,
      'css' => $css,
    ];
    foreach ($files as $type => $file_path) {

      // Evaluate if the path are an local or external.
      $is_local = parse_url($file_path, PHP_URL_SCHEME) === NULL && strpos($file_path, '//') !== 0;

      // In that location $file_path are placed on root of module.
      // not in drupal root.
      $data = $is_local && substr($file_path, 0, 1) !== '/' ? '/' . $file_path : $file_path;
      if ($type === 'js') {
        $libraries['cropper'][$type][$data] = [
          'type' => $is_local ? 'file' : 'external',
          'minified' => TRUE,
        ];
      }
      else {
        $libraries['cropper'][$type]['component'][$data] = [
          'type' => $is_local ? 'file' : 'external',
          'minified' => TRUE,
        ];
      }
    }
  }
  elseif (\Drupal::moduleHandler()
    ->moduleExists('libraries') && ($info = libraries_detect('cropper')) && $info['installed']) {
    $libraries['cropper']['version'] = $info['version'];
    foreach ($info['files'] as $type => $files) {

      // Fetch all possible entry.
      foreach ($files as $data => $option) {
        if (is_numeric($data)) {
          $option = "/{$info['library path']}/{$option}";
        }
        elseif (empty($option['type']) || $option['type'] == 'file') {
          $data = "/{$info['library path']}/{$data}";
        }
      }
      if ($type == 'css') {
        $libraries['cropper']['css']['theme'][$data] = $option;
      }
      else {
        $libraries['cropper'][$type][$data] = $option;
        $libraries['cropper'][$type][$data] = $option;
      }
    }
  }
  else {

    // Fallback to CDN.
    $js = IMAGE_WIDGET_CROP_JS_CDN;
    $libraries['cropper']['js'][$js] = [
      'type' => 'external',
      'minified' => TRUE,
    ];
    $css = IMAGE_WIDGET_CROP_CSS_CDN;
    $libraries['cropper']['css']['component'][$css] = [
      'type' => 'external',
      'minified' => TRUE,
    ];
    $libraries['cropper']['version'] = 'web-hosted';
  }
}