You are here

function jquery_calendar_library_info_alter in jQuery World Calendars API 3.x

@todo automatcally detect available calendar types, code exists but it's buggy Implements hook_library_info_alter

Parameters

$libraries:

$extension:

File

./jquery_calendar.module, line 111
Implements necessary hooks, API and helpers for jQuery World Calendars.

Code

function jquery_calendar_library_info_alter(&$libraries, $extension) {
  if (isset($libraries['jquery_calendars.core'])) {
    $path = DRUPAL_ROOT . dirname(dirname(key($libraries['jquery_calendars.core']['js'])));
    $files = \Drupal::service('file_system')
      ->scanDirectory($path . '/js', '/.*/');
    foreach ($files as $file) {

      /*
      # Add any calendar type that is missing from the libraries.yml file
      if (preg_match('%jquery\.calendars(\.([a-z]+))$%i', $file->name, $matches)) {
        $name = $matches[2];
        if ($name !== 'lang' && $name !== 'min' && $name !== 'picker' && $name !== 'all' && $name !== 'validation' && $name !== 'plus') {
          $libraries['jquery_calendars.type.' . $name] = $libraries['jquery_calendars.type.coptic'];
          $oldFile = key($libraries['jquery_calendars.type.' . $name]['js']);
          $newFile = str_replace('coptic', $name, $oldFile);
          $libraries['jquery_calendars.type.' . $name]['js'][$newFile] = $libraries['jquery_calendars.type.coptic']['js'][$oldFile];
          unset($libraries['jquery_calendars.type.' . $name]['js'][$oldFile]);
          var_Dump($libraries['jquery_calendars.type.' . $name]);
        }
      }
      */

      # Add any calendar lang that is missing from the libraries.yml file
      if (preg_match('%jquery\\.calendars(\\.([a-z]+))?-([a-z\\-]+)%i', $file->name, $matches)) {
        $lang = $matches[3];
        $type = $matches[2];
        $sample = NULL;
        if (empty($type)) {
          $type = 'core';
          $sample = 'core-fa';
        }
        elseif ($type === 'picker') {
          $sample = 'picker-fa';
        }
        else {
          $sample = 'type.persian-fa';
          $type = 'type.' . $type;
        }
        $libraries["jquery_calendars.lang.{$type}-{$lang}"] = $libraries["jquery_calendars.lang.{$sample}"];
        $libraries["jquery_calendars.lang.{$type}-{$lang}"]['js'] = [
          str_replace(DRUPAL_ROOT, '', $file->uri) => [],
        ];
      }
    }

    # Find the files with minify version and update the library paths if needed
    $compressionLevel = \Drupal::config('jquery_calendar.settings')
      ->get('compression_level');
    if ($compressionLevel === 'min') {
      foreach ($libraries as $key => $library) {
        foreach ($library['js'] as $file => $info) {
          if (preg_match('%jquery_calendars.*%', $key) && isset($library['js'])) {
            $minifiedPath = preg_replace('/(.*)\\.js$/', '$1.min.js', $file);
            if (file_exists(DRUPAL_ROOT . $minifiedPath)) {
              $libraries[$key]['js'][$minifiedPath] = $library['js'][$file];
              $libraries[$key]['js'][$minifiedPath]['minified'] = true;
              unset($libraries[$key]['js'][$file]);
            }
          }
        }
      }
    }
  }
}