You are here

public function CssCollectionRenderer::render in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Asset/CssCollectionRenderer.php \Drupal\Core\Asset\CssCollectionRenderer::render()

Renders an asset collection.

Parameters

array $assets: An asset collection.

Return value

array A render array to render the asset collection.

Overrides AssetCollectionRendererInterface::render

File

core/lib/Drupal/Core/Asset/CssCollectionRenderer.php, line 47

Class

CssCollectionRenderer
Renders CSS assets.

Namespace

Drupal\Core\Asset

Code

public function render(array $css_assets) {
  $elements = [];

  // A dummy query-string is added to filenames, to gain control over
  // browser-caching. The string changes on every update or full cache
  // flush, forcing browsers to load a new copy of the files, as the
  // URL changed.
  $query_string = $this->state
    ->get('system.css_js_query_string', '0');

  // Defaults for LINK and STYLE elements.
  $link_element_defaults = [
    '#type' => 'html_tag',
    '#tag' => 'link',
    '#attributes' => [
      'rel' => 'stylesheet',
    ],
  ];
  foreach ($css_assets as $css_asset) {
    $element = $link_element_defaults;
    $element['#attributes']['media'] = $css_asset['media'];
    $element['#browsers'] = $css_asset['browsers'];
    switch ($css_asset['type']) {

      // For file items, output a LINK tag for file CSS assets.
      case 'file':
        $element['#attributes']['href'] = $this->fileUrlGenerator
          ->generateString($css_asset['data']);

        // Only add the cache-busting query string if this isn't an aggregate
        // file.
        if (!isset($css_asset['preprocessed'])) {
          $query_string_separator = strpos($css_asset['data'], '?') !== FALSE ? '&' : '?';
          $element['#attributes']['href'] .= $query_string_separator . $query_string;
        }
        break;
      case 'external':
        $element['#attributes']['href'] = $css_asset['data'];
        break;
      default:
        throw new \Exception('Invalid CSS asset type.');
    }

    // Merge any additional attributes.
    if (!empty($css_asset['attributes'])) {
      $element['#attributes'] += $css_asset['attributes'];
    }
    $elements[] = $element;
  }
  return $elements;
}