CssCollectionRenderer.php in Drupal 10
File
core/lib/Drupal/Core/Asset/CssCollectionRenderer.php
View source
<?php
namespace Drupal\Core\Asset;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\State\StateInterface;
class CssCollectionRenderer implements AssetCollectionRendererInterface {
protected $state;
protected $fileUrlGenerator;
public function __construct(StateInterface $state, FileUrlGeneratorInterface $file_url_generator) {
$this->state = $state;
$this->fileUrlGenerator = $file_url_generator;
}
public function render(array $css_assets) {
$elements = [];
$query_string = $this->state
->get('system.css_js_query_string', '0');
$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'];
switch ($css_asset['type']) {
case 'file':
$element['#attributes']['href'] = $this->fileUrlGenerator
->generateString($css_asset['data']);
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.');
}
if (!empty($css_asset['attributes'])) {
$element['#attributes'] += $css_asset['attributes'];
}
$elements[] = $element;
}
return $elements;
}
}