JsCollectionRenderer.php 3.4 KB

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