You are here

function _yamlform_library_info_alter_recursive in YAML Form 8

Recursive through a form library.

Parameters

array $library: A form library defined in yamlform.libraries.yml.

array $cdn: A associative array of library paths mapped to CDN URL.

1 call to _yamlform_library_info_alter_recursive()
yamlform_library_info_alter in includes/yamlform.libraries.inc
Implements hook_library_info_alter().

File

includes/yamlform.libraries.inc, line 43
Form libraries.

Code

function _yamlform_library_info_alter_recursive(array &$library, array $cdn) {
  foreach ($library as $key => &$value) {

    // CSS and JS files and listed in associative arrays keyed via string.
    if (!is_string($key) || !is_array($value)) {
      continue;
    }

    // Ignore the CDN's associative array.
    if ($key == 'cdn') {
      continue;
    }

    // Replace the CDN sources (ie /library/*) with the CDN URL destination
    // (https://cdnjs.cloudflare.com/ajax/libs/*).
    foreach ($cdn as $source => $destination) {
      if (strpos($key, $source) === 0) {
        $uri = str_replace($source, $destination, $key);
        $library[$uri] = $value;
        unset($library[$key]);
        break;
      }
    }

    // Recurse downward to find nested libraries.
    _yamlform_library_info_alter_recursive($value, $cdn);
  }
}