function libraries_cdn_add in Libraries CDN API 7
Function to add a library to a page.
Parameters
mixed $cdns: The cdn name or an array of CDN name.
string $library: The library name.
mixed $version: The version of the library. If not set it will detect the latest.
array $configuration: The configuration to pass to the object.
1 string reference to 'libraries_cdn_add'
- libraries_cdn_load in ./
libraries_cdn.module - Use the #attached key of a render array to attach a cdn libraries.
File
- ./
libraries_cdn.module, line 174 - Main module file.
Code
function libraries_cdn_add($cdns = array(), $library = NULL, $version = NULL, array $configuration = array()) {
if (!isset($library)) {
return;
}
if (is_string($cdns)) {
$cdns = (array) $cdns;
}
if (empty($cdns) || in_array('*', $cdns)) {
// @codingStandardsIgnoreStart
$cdns = \Drupal\libraries_cdn\LibrariesCDN::getAvailableCDN();
// @codingStandardsIgnoreEnd
}
if (empty($configuration)) {
$configuration = array(
'request' => array(
'timeout' => 10,
),
);
}
$found = FALSE;
// Speed improvement, check the cache before going to the loop.
if ($cache = cache_get(implode('', $cdns) . ':' . $library . ':' . $version)) {
$results = $cache->data;
$cdns = array();
}
foreach ($cdns as $cdn) {
if ($found == TRUE) {
continue;
}
// @codingStandardsIgnoreStart
if (\Drupal\libraries_cdn\LibrariesCDN::isAvailableCDN($cdn)) {
\Drupal\libraries_cdn\LibrariesCDN::setPlugin($cdn, $library, $configuration);
if (\Drupal\libraries_cdn\LibrariesCDN::isAvailable()) {
$user_version = $version ? $version : \Drupal\libraries_cdn\LibrariesCDN::getLatestVersion();
// @codingStandardsIgnoreEnd
if ($cache = cache_get($cdn . ':' . $library . ':' . $user_version)) {
$results = $cache->data;
$found = TRUE;
}
else {
// @codingStandardsIgnoreStart
$files = \Drupal\libraries_cdn\LibrariesCDN::getFiles();
// @codingStandardsIgnoreEnd
if (isset($files[$user_version])) {
foreach ((array) $files[$user_version] as $file) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if (strpos($file, 'debug') !== FALSE || strpos($file, 'min') !== FALSE) {
continue;
}
$results[$ext][] = $file;
$found = TRUE;
}
$cache_id = $cdn . ':' . $library . ':' . $user_version;
cache_set($cache_id, $results);
}
}
}
}
}
if (!empty($results)) {
foreach ($results as $type => $files) {
foreach ($files as $file) {
call_user_func_array('drupal_add_' . $type, array(
$file,
array(
'type' => 'external',
),
));
}
}
}
}