You are here

function libraries_cdn_libraries_info_alter in Libraries CDN API 8

Same name and namespace in other branches
  1. 7 libraries_cdn.module \libraries_cdn_libraries_info_alter()

Implements hook_libraries_info_alter().

To get into this hook, add a new key to the library definition in hook_libraries_info().

Here's an example:

'cdn' => array( 'aliases' => array('ol3', 'openlayers3'), 'limit' => 3, 'options' => array( 'weight' => 0, 'group' => $js_css_group, ), 'download' => array( 'versions' => array('3.8.1'), 'plugins' => array( 'cdnjs' => array('3.8.2') '*' => array('latest') ), ), 'request' => array( 'timeout' => 5, ) )

  • plugins: array, the list of cdn plugins to search the library from. It Will use all of them if not set.
  • aliases: array, if the library has different names.
  • limit: integer, set this to limit the number of results. If set to 3, it will return the 3 latest versions available.
  • options: array, this array will be applied to each file definition, see drupal_add_TYPE() (js or css) to see which are the keys.
  • download: array, options to download a local copy of the library
    • versions: array, version to download on any CDN when available.
    • plugins: array, keys are CDN plugin ids and the special keyword '*' can be use to specify all CDN plugins. Values are versions to download when available. The special keyword: 'latest' can be used to download the latest version available.
  • request: array, this array will be the configuration that will be passed to the request function. See drupal_http_request() for a list of key values.

File

./libraries_cdn.module, line 52
Main module file.

Code

function libraries_cdn_libraries_info_alter(&$info) {
  foreach ($info as $library_name => &$library) {
    $variants = array();
    if (isset($library['cdn'])) {
      if (isset($library['cdn']['processed'])) {
        continue;
      }
      $options = $library['cdn'];
      if (!isset($options['options'])) {
        $options['options'] = array();
      }
      $available_plugins = \Drupal\libraries_cdn\LibrariesCdn::getAvailableCDN();
      if (!isset($options['plugins']) || empty($options['plugins'])) {
        $options['plugins'] = $available_plugins;
      }
      else {
        $options['plugins'] = array_filter($options['filter'], function ($value) use ($available_plugins) {
          return in_array($value, $available_plugins);
        });
      }
      if (!isset($options['aliases'])) {
        $options['aliases'] = array();
      }
      $options['aliases'] += array(
        $library_name,
      );
      $options['aliases'] = array_unique($options['aliases']);
      foreach ($options['aliases'] as $alias) {
        foreach ($options['plugins'] as $plugin) {
          \Drupal\libraries_cdn\LibrariesCdn::setPlugin($plugin, $alias, $options);
          $variants += \Drupal\libraries_cdn\LibrariesCdn::getLibrariesVariants();
        }
      }
      $library['variants'] += $variants;
    }
    $library['cdn']['processed'] = TRUE;
  }
}