function public function CdnAssets::toLibraryArray

8.x-3.x CdnAssets.php public CdnAssets::toLibraryArray($minified = NULL)

Converts the CDN Assets into an array suitable for a Drupal library array.

Parameters

bool $minified: Flag indicating whether to use minified assets.

Return value

array An array structured for use in a Drupal library.

Class

CdnAssets
Class CdnAssets.

Namespace

Drupal\bootstrap\Plugin\Provider

Source src/Plugin/Provider/CdnAssets.php (line 289)

public function toLibraryArray($minified = NULL) {
  $assets = $this->all($minified);
  $library = [];

  // Iterate over each type.
  foreach ($assets as $asset) {
    $url = (string) $asset;
    $type = $asset->getType();
    $data = ['data' => $url, 'type' => 'external'];

    // Attempt to add a corresponding SRI attribute for the URL.
    // @see https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity
    foreach (['sha512', 'sha384', 'sha256', 'sha', 'hash', 'sri', 'integrity'] as $key) {
      if ($integrity = $asset->getInfo($key)) {
        // Parse the SRI integrity value to extract both the algorithm and
        // hash. Note: this is needed as some APIs do not prepend the hash
        // with the actual algorithm used. This is likely because the field,
        // while a valid base64 encoded hash, isn't specifically intended for
        // use as an SRI integrity attribute value.
        list($algorithm, $hash) = Crypt::parseSriIntegrity($integrity);

        // Ensure the algorithm and hash are valid.
        if (Crypt::checkBase64HashAlgorithm($algorithm, $hash, TRUE)) {
          $data['attributes'] = [
            'integrity' => "$algorithm-$hash",
            'crossorigin' => $asset->getInfo('crossorigin', 'anonymous'),
          ];
        }
        break;
      }
    }

    // CSS library assets use "SMACSS" categorization, assign to "base".
    if ($type === 'css') {
      $library[$type]['base'][$url] = $data;
    }
    else {
      $library[$type][$url] = $data;
    }
  }

  return $library;
}