From 36f1473ffab84b1902ca6396ff2d6fc8456fc71b Mon Sep 17 00:00:00 2001
From: Geoff Appleby <geoff@gapple.ca>
Date: Fri, 28 Aug 2020 12:31:50 -0700
Subject: [PATCH] Issue #3167803 by bburg, gapple: Allow attributes on elements

---
 readme.md                                    | 3 +++
 src/Asset/CssCollectionRendererDecorator.php | 5 +++++
 src/Asset/JsCollectionRendererDecorator.php  | 5 +++++
 3 files changed, 13 insertions(+)

diff --git a/readme.md b/readme.md
index 167b6c9..82b6367 100644
--- a/readme.md
+++ b/readme.md
@@ -25,6 +25,9 @@ $render['element'] = [
       [
         'data'  => '.highlight { background-color: yellow; }',
         'group' => CSS_THEME,
+        'attributes' => [
+          'media' => 'all',
+        ]
       ],
     ],
   ],
diff --git a/src/Asset/CssCollectionRendererDecorator.php b/src/Asset/CssCollectionRendererDecorator.php
index d664ed4..1754c29 100644
--- a/src/Asset/CssCollectionRendererDecorator.php
+++ b/src/Asset/CssCollectionRendererDecorator.php
@@ -76,6 +76,7 @@ class CssCollectionRendererDecorator implements AssetCollectionRendererInterface
       '#type' => 'html_tag',
       '#tag' => 'style',
       '#value' => '',
+      '#attributes' => [],
     ];
 
     // Loop through all JS assets.
@@ -87,6 +88,10 @@ class CssCollectionRendererDecorator implements AssetCollectionRendererInterface
       $element = $element_defaults;
       $element['#value'] = AttachInlineMarkup::create($css_asset['data']);
 
+      if (!empty($css_asset['attributes'])) {
+        $element['#attributes'] += $css_asset['attributes'];
+      }
+
       if ($this->moduleHandler->moduleExists('csp')) {
         $allowMethod = $this->config->get('attachinline.settings')->get('csp-allow-method') ?? 'hash';
         if ($allowMethod == 'nonce') {
diff --git a/src/Asset/JsCollectionRendererDecorator.php b/src/Asset/JsCollectionRendererDecorator.php
index 198af56..6339402 100644
--- a/src/Asset/JsCollectionRendererDecorator.php
+++ b/src/Asset/JsCollectionRendererDecorator.php
@@ -76,6 +76,7 @@ class JsCollectionRendererDecorator implements AssetCollectionRendererInterface
       '#type' => 'html_tag',
       '#tag' => 'script',
       '#value' => '',
+      '#attributes' => [],
     ];
 
     // Loop through all JS assets.
@@ -87,6 +88,10 @@ class JsCollectionRendererDecorator implements AssetCollectionRendererInterface
       $element = $element_defaults;
       $element['#value'] = AttachInlineMarkup::create($js_asset['data']);
 
+      if (!empty($js_asset['attributes'])) {
+        $element['#attributes'] += $js_asset['attributes'];
+      }
+
       if ($this->moduleHandler->moduleExists('csp')) {
         $allowMethod = $this->config->get('attachinline.settings')->get('csp-allow-method') ?? 'hash';
         if ($allowMethod == 'nonce') {
-- 
GitLab