blob: 162a7bccbe8c0659710ddaac2b975819abd1012f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
<?php
namespace Drupal\Core\Asset;
use Drupal\Core\File\FileUrlGeneratorInterface;
/**
* Renders CSS assets.
*/
class CssCollectionRenderer implements AssetCollectionRendererInterface {
/**
* Constructs a CssCollectionRenderer.
*
* @param \Drupal\Core\Asset\AssetQueryStringInterface $assetQueryString
* The asset query string.
* @param \Drupal\Core\File\FileUrlGeneratorInterface $fileUrlGenerator
* The file URL generator.
*/
public function __construct(
protected AssetQueryStringInterface $assetQueryString,
protected FileUrlGeneratorInterface $fileUrlGenerator,
) {
}
/**
* {@inheritdoc}
*/
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->assetQueryString->get();
// 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'];
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 = str_contains($css_asset['data'], '?') ? '&' : '?';
$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;
}
}
|