You are here

function jw_player_library_info_alter in JW Player 8

Implements hook_library_info_alter().

File

./jw_player.module, line 157
Adds a theme function which allows theme developers to use the JW Player.

Code

function jw_player_library_info_alter(&$libraries, $extension) {
  if ($extension != 'jw_player') {
    return;
  }
  $config = \Drupal::config('jw_player.settings');
  if ($cloud_hosted_default = $config
    ->get('cloud_player_library_url')) {

    // Cloud hosted player, use external JavaScript
    $libraries['jwplayer']['version'] = 'cloud-hosted';
    $libraries['jwplayer']['js'] = [
      $cloud_hosted_default => [
        'type' => 'external',
      ],
    ];
    return;
  }

  // If version 7 is selected, look for a separate jwplayer7 library
  // for this. Prefer that if it exists. This allows to have two versions
  // of JW Player on the site, one for each major version.
  $version_7_path = NULL;
  if ($config
    ->get('jw_player_version') == '7' && $config
    ->get('jw_player_key')) {

    // Check whether the library version 7 exists in library folder.
    if (\Drupal::hasService('library.libraries_directory_file_finder')) {

      /** @var \Drupal\Core\Asset\LibrariesDirectoryFileFinder $library_file_finder */
      $library_file_finder = \Drupal::service('library.libraries_directory_file_finder');
      $version_7_path = $library_file_finder
        ->find('jwplayer7');
    }
    elseif (\Drupal::moduleHandler()
      ->moduleExists('libraries')) {
      $version_7_path = libraries_get_path('jwplayer7');
    }
    else {
      $version_7_path = 'libraries/jwplayer7';
    }
  }
  if (\Drupal::moduleHandler()
    ->moduleExists('libraries')) {
    $info = libraries_detect('jwplayer');
    $libraries['jwplayer'] += array(
      'website' => $info['vendor url'],
      'version' => $info['installed'] ? $info['version'] : 'cloud-hosted',
    );
    if ($version_7_path && file_exists($version_7_path)) {
      $info['library path'] = $version_7_path;

      // Detect full version string for version 7.
      $info['version arguments']['pattern'] = '/return"(7\\.[0-9][0-9]?\\.[0-9][0-9]?)/';

      // First 100kb should be more then enough to reach version string for
      // version 7, which is positioned in first half of the file.
      $info['version arguments']['lines'] = 2;
      $info['version arguments']['cols'] = 50000;
      $info['version'] = libraries_get_version($info, $info['version arguments']);
      $info['installed'] = TRUE;
    }
    if ($info['installed']) {
      $libraries['jwplayer']['library path'] = $info['library path'];

      // Self hosted player, use files from library definition.
      $libraries['jwplayer']['js'] = [];
      if (!empty($info['files']['js'])) {
        foreach ($info['files']['js'] as $data => $option) {
          if (is_numeric($data)) {
            $option = "/{$info['library path']}/{$option}";
          }
          elseif (empty($option['type']) || $option['type'] == 'file') {
            $data = "/{$info['library path']}/{$data}";
          }
          $libraries['jwplayer']['js'][$data] = $option;
        }
      }
    }

    // Use integration files from library definition.
    foreach ($info['integration files'] as $module => $files) {
      foreach (array_keys($files) as $type) {
        $module_path = drupal_get_path('module', $module);
        foreach ($files[$type] as $data => $option) {
          if (is_numeric($data)) {
            $option = "{$module_path}/{$option}";
          }
          elseif (empty($option['type']) || $option['type'] == 'file') {
            $data = "{$module_path}/{$data}";
          }
          $libraries['jwplayer'][$type][$data] = $option;
        }
      }
    }
  }
  elseif (\Drupal::hasService('library.libraries_directory_file_finder')) {
    if ($version_7_path && file_exists($version_7_path)) {
      $libraries['jwplayer']['js'] = [
        '/' . $version_7_path . '/jwplayer.js' => [
          'type' => 'external',
        ],
      ];
    }
  }
}