Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Asset;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Serialization\Json;
|
Chris@0
|
6 use Drupal\Core\State\StateInterface;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Renders JavaScript assets.
|
Chris@0
|
10 */
|
Chris@0
|
11 class JsCollectionRenderer implements AssetCollectionRendererInterface {
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * The state key/value store.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @var \Drupal\Core\State\StateInterface
|
Chris@0
|
17 */
|
Chris@0
|
18 protected $state;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * Constructs a JsCollectionRenderer.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @param \Drupal\Core\State\StateInterface $state
|
Chris@0
|
24 * The state key/value store.
|
Chris@0
|
25 */
|
Chris@0
|
26 public function __construct(StateInterface $state) {
|
Chris@0
|
27 $this->state = $state;
|
Chris@0
|
28 }
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * {@inheritdoc}
|
Chris@0
|
32 *
|
Chris@0
|
33 * This class evaluates the aggregation enabled/disabled condition on a group
|
Chris@0
|
34 * by group basis by testing whether an aggregate file has been made for the
|
Chris@0
|
35 * group rather than by testing the site-wide aggregation setting. This allows
|
Chris@0
|
36 * this class to work correctly even if modules have implemented custom
|
Chris@0
|
37 * logic for grouping and aggregating files.
|
Chris@0
|
38 */
|
Chris@0
|
39 public function render(array $js_assets) {
|
Chris@0
|
40 $elements = [];
|
Chris@0
|
41
|
Chris@0
|
42 // A dummy query-string is added to filenames, to gain control over
|
Chris@0
|
43 // browser-caching. The string changes on every update or full cache
|
Chris@0
|
44 // flush, forcing browsers to load a new copy of the files, as the
|
Chris@0
|
45 // URL changed. Files that should not be cached get REQUEST_TIME as
|
Chris@0
|
46 // query-string instead, to enforce reload on every page request.
|
Chris@0
|
47 $default_query_string = $this->state->get('system.css_js_query_string') ?: '0';
|
Chris@0
|
48
|
Chris@0
|
49 // Defaults for each SCRIPT element.
|
Chris@0
|
50 $element_defaults = [
|
Chris@0
|
51 '#type' => 'html_tag',
|
Chris@0
|
52 '#tag' => 'script',
|
Chris@0
|
53 '#value' => '',
|
Chris@0
|
54 ];
|
Chris@0
|
55
|
Chris@0
|
56 // Loop through all JS assets.
|
Chris@0
|
57 foreach ($js_assets as $js_asset) {
|
Chris@0
|
58 // Element properties that do not depend on JS asset type.
|
Chris@0
|
59 $element = $element_defaults;
|
Chris@0
|
60 $element['#browsers'] = $js_asset['browsers'];
|
Chris@0
|
61
|
Chris@0
|
62 // Element properties that depend on item type.
|
Chris@0
|
63 switch ($js_asset['type']) {
|
Chris@0
|
64 case 'setting':
|
Chris@0
|
65 $element['#attributes'] = [
|
Chris@0
|
66 // This type attribute prevents this from being parsed as an
|
Chris@0
|
67 // inline script.
|
Chris@0
|
68 'type' => 'application/json',
|
Chris@0
|
69 'data-drupal-selector' => 'drupal-settings-json',
|
Chris@0
|
70 ];
|
Chris@0
|
71 $element['#value'] = Json::encode($js_asset['data']);
|
Chris@0
|
72 break;
|
Chris@0
|
73
|
Chris@0
|
74 case 'file':
|
Chris@0
|
75 $query_string = $js_asset['version'] == -1 ? $default_query_string : 'v=' . $js_asset['version'];
|
Chris@0
|
76 $query_string_separator = (strpos($js_asset['data'], '?') !== FALSE) ? '&' : '?';
|
Chris@0
|
77 $element['#attributes']['src'] = file_url_transform_relative(file_create_url($js_asset['data']));
|
Chris@0
|
78 // Only add the cache-busting query string if this isn't an aggregate
|
Chris@0
|
79 // file.
|
Chris@0
|
80 if (!isset($js_asset['preprocessed'])) {
|
Chris@0
|
81 $element['#attributes']['src'] .= $query_string_separator . ($js_asset['cache'] ? $query_string : REQUEST_TIME);
|
Chris@0
|
82 }
|
Chris@0
|
83 break;
|
Chris@0
|
84
|
Chris@0
|
85 case 'external':
|
Chris@0
|
86 $element['#attributes']['src'] = $js_asset['data'];
|
Chris@0
|
87 break;
|
Chris@0
|
88
|
Chris@0
|
89 default:
|
Chris@0
|
90 throw new \Exception('Invalid JS asset type.');
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 // Attributes may only be set if this script is output independently.
|
Chris@0
|
94 if (!empty($element['#attributes']['src']) && !empty($js_asset['attributes'])) {
|
Chris@0
|
95 $element['#attributes'] += $js_asset['attributes'];
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 $elements[] = $element;
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 return $elements;
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 }
|