Problem/Motivation

Currently independent JS libraries (script loaded via its own SCRIPT tag) have the ability to add custom attributes to the tag and this is not the case for CSS libraries of the same type (scripts loaded via their own LINK tag).

# MODULE.libraries.yml or THEME.libraries.yml
bootstrap.cdn:
  header: true
  remote: https://github.com/twbs/bootstrap
  version: '3.3.6'
  license:
    name: MIT
    url: https://github.com/twbs/bootstrap/blob/master/LICENSE
	 	gpl-compatible: true
  css:
    component:
      'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css': { type: external, attributes: { integrity: sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7, crossorigin: anonymous }, minified: true }
  js:
    'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js': { type: external, attributes: { integrity: sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS, crossorigin: anonymous }, minified: true }
  dependencies:
    - core/jquery

This would be helpful when including libraries like Bootstrap:

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
	 	 
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">	

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

Proposed resolution

Add similar functionality to the CssCollectionRenderer class as implemented by the JsCollectionRenderer class.

CommentFileSizeAuthor
#104 2716115-91.patch5.25 KBalexpott
#94 2716115-94.patch5.44 KBksemihin
#91 2716115-91.patch5.25 KBgapple
#91 interdiff-2716115-91.txt669 bytesgapple
#88 interdiff-2716115-74-87.txt691 bytesgnuget
#87 2716115-87.patch5.34 KBmrinalini9
#4 drupal-2716115-4.patch2.4 KBLittleCoding
#6 drupal_8_3_x-2716115-6.patch2.4 KBLittleCoding
#7 drupal_8_3_x-2716115-7.patch2.4 KBLittleCoding
#6 drupal_8_4_x-2716115-6.patch2.4 KBLittleCoding
#8 drupal_8_4_x-2716115-8.patch2.4 KBLittleCoding
#12 drupal-2716115-12.patch2.48 KBLittleCoding
#14 drupal-2716115-14.patch2.23 KBLittleCoding
#23 Screen Shot 2019-02-23 at 2.50.37 PM.png93.61 KBmarkhalliwell
#23 Screen Shot 2019-02-23 at 2.51.43 PM.png51.49 KBmarkhalliwell
#36 drupal-2716115-36.patch761 byteserlendoos
#37 2716115-37.patch707 bytestatarbj
#39 2716115-37-39-interdiff.txt3.24 KBgnuget
#39 2716115-39.patch3.06 KBgnuget
#46 allow_attributes_passed_with_CSS_in_librarie-2716115-46.patch1.02 KBLittleCoding
#47 allow_attributes_passed_with_CSS_in_libraries-2716115-47.patch1.09 KBLittleCoding
#48 allow_attributes_passed_with_CSS_in_libraries-2716115-48.patch1.14 KBLittleCoding
#54 2716115-39-54-interdiff.txt790 bytesgnuget
#54 2716115-54.patch3.06 KBgnuget
#63 2716115-63.patch693 bytessandeep_jangra
#69 allow_attributes_passed_with_css_in_libraries-2716115-69.patch3.04 KBLittleCoding
#73 2716115-73.patch3.53 KBjungle
#73 interdiff-69-73.txt1.89 KBjungle
#74 2716115-74.patch5.38 KBjungle
#74 interdiff-73-74.txt2.18 KBjungle
#78 interdiff-2716115-78.patch671 bytesgapple
#78 drupal-2716115-78-css-attributes.patch5.26 KBgapple
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

LittleCoding created an issue. See original summary.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

LittleCoding’s picture

Category: Feature request » Bug report
Status: Active » Needs review
Issue tags: +Novice, +Needs tests
FileSize
2.4 KB

Patch is designed to work with 8.2.x+

Status: Needs review » Needs work

The last submitted patch, 4: drupal-2716115-4.patch, failed testing.

LittleCoding’s picture

FileSize
2.4 KB
2.4 KB
LittleCoding’s picture

Let me report those patches as they did not get queued for testing.

Here is the patch for 8.3.x

LittleCoding’s picture

LittleCoding’s picture

Status: Needs work » Needs review

The last submitted patch, 7: drupal_8_3_x-2716115-7.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 8: drupal_8_4_x-2716115-8.patch, failed testing.

LittleCoding’s picture

Status: Needs review » Needs work

The last submitted patch, 12: drupal-2716115-12.patch, failed testing.

LittleCoding’s picture

Status: Needs work » Needs review

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

LittleCoding’s picture

Category: Bug report » Feature request

Updated Category to better fit the issue.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

cayriawill’s picture

Adding relationship to another issue that is the same

LittleCoding’s picture

Issue summary: View changes

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

markhalliwell’s picture

Title: Allow attributes in independent CSS script LINK tags » Allow attributes passed with CSS in libraries (SRI)
Status: Needs review » Needs work

Setting back to CNW for tests.

markhalliwell’s picture

This related issue is the 7.x equivalent.

I've been working on #2851748: Secure sites by adding attributes "integrity" and "crossorigin" to CDN files and can confirm that attributes passed to JS already works:

However, CSS does not:

LittleCoding’s picture

@markcarver are you confirming the issue still exists or are you identifying that the patch #14 no longer works?

Also thank you for the title update. Correct terms will help to get this noticed and accepted.

markhalliwell’s picture

I'm confirming that the issue still exists in HEAD. I haven't actually tried or really reviewed the patch, yet.

I'm just adding this to the scope of issues that really needs to be resolved.

I'm half tempted to mark this as a task or even as a bug since this directly affects the ability (natively) to provide proper SRI values for external resources. Technically speaking though, this adds new [desired] functionality that previously wasn't possible... so I suppose I'll leave it as a feature request; for now. If someone else feels this needs recategorization, please mention it here so we can see how we want to move forward.

LittleCoding’s picture

I went over the thoughts when first making this request. And actually it was making a custom theme based on Bootstrap that is how I came across the need.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

DuaelFr’s picture

The official documentation says that this should be possible. I'd aggree to consider this as a bug :)

My 2 cts.

LittleCoding’s picture

@markcarver do you know anyone that is running a sprint at DrupalCon 2019 and would be interested in add this issue to their list?

gobinathm’s picture

Issue tags: +Seattle2019

Tagging for Seattle sprints

LittleCoding’s picture

Status: Needs work » Needs review
Issue tags: -Novice
markhalliwell’s picture

Category: Feature request » Bug report
Priority: Normal » Major
Status: Needs review » Needs work
Issue tags: -Seattle2019

@LittleCoding, sorry, this wasn't on my radar while I was in Seattle.

That being said, seeing how nothing happen in Seattle with this issue, removing the tag.

Changing to a bug and bumping priority since existing documentation conflicts with actual behavior (per #28).

Setting back to CNW since there are no tests.

LittleCoding’s picture

@markcarver sounds like a good idea +1

tatarbj’s picture

Issue tags: +DrupalCampBelarus2019
LittleCoding’s picture

@tatarbj thank you +1

erlendoos’s picture

New patch for drupal 8.7

tatarbj’s picture

gnuget’s picture

Status: Needs review » Needs work

Thanks!

This still needs tests, so I'm going to switch it back to Needs Work I'm going to try to work on the tests this week.

gnuget’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
3.24 KB
3.06 KB

Ok, new patch, the only change that I did to #37 (besides to add the test) was that I changed the array_replace parameters order, it was moving the rel, media and href to after the custom attributes making the tag look different than the others link tags.

So with the previous version, it was printing the tag like this:

<link integrity="sha384-oS3vJWv+0UjzBfQzYXAZQ7Ay" crossorigin="anonymous" rel="stylesheet" media="all" href="//use.something.css" />

And with my change it is printing it like this:

<link rel="stylesheet" media="all" href="//use.something.css" integrity="sha384-oS3vJWv+0UjzBfQzYXAZQ7Ay" crossorigin="anonymous" />

And that's it.

Thanks for all the help on this one.

David.

(why was added the "api change" tag )

LittleCoding’s picture

Looks like we have two fixes now (current and upcoming versions of core).

To clarify for those following along:

The patch from comment #14 is for 8.6.x and blow
The patch form comments #37/#39 are for 8.7.x

Since we are working with this as a bug report, rather then a feature request I have updated the version back to 8.7.x-dev

markhalliwell’s picture

Version: 8.7.x-dev » 8.8.x-dev

HEAD is always fixed first. Whether or not the fix is backported to previous versions is irrelevant.

LittleCoding’s picture

Thank you for the correction @markcarver and more so for the information on why!

NickDickinsonWilde’s picture

Status: Needs review » Needs work

Reviewed it and I have a questions, and a bug.
Tests look good to me.

      // Attributes may only be set if this stylesheet is output independently
      // as a LINK element.
      if (!empty($css_asset['attributes'])) {
        $element['#attributes'] = array_replace($element['#attributes'], $css_asset['attributes']);
      }

Bug: This doesn't check if the css is a group (ie preprocessed) - adjusting the if statement to if (!$css_asset['preprocess'] && !empty($css_asset['attributes'])) { should resolve this.

Question: Why use array_replace instead of +=? += is used in the equivalent functionality for JS, and is a tiny bit better performance (given caching that's not really a major concern and it is a minor performance difference I believe)

gnuget’s picture

Question: Why use array_replace instead of +=? += is used in the equivalent functionality for JS, and is a tiny bit better performance (given caching that's not really a major concern and it is a minor performance difference I believe)

No idea, I'm going to change it in my next patch.

I added the changed that you suggested and it make the test to fail. I will try to dig a bit more on this later this week.

Thanks for your review!

Testing Drupal\Tests\Core\Asset\CssCollectionRendererUnitTest
..F...                                                              6 / 6 (100%)

Time: 5.26 seconds, Memory: 4.00MB

There was 1 failure:

1) Drupal\Tests\Core\Asset\CssCollectionRendererUnitTest::testRender with data set #2 (array(array(0, 'file', 'all', true, 'public://css/file-all', array(), array('sha384-psK1OYPAYjYUhtDYW+Pj2yc', 'anonymous', 'test'))), array(array('html_tag', 'link', array('stylesheet', 'all', 'file_url_transform_relative:f...-all?0', 'sha384-psK1OYPAYjYUhtDYW+Pj2yc', 'anonymous', 'test'), array())))
Failed asserting that Array &0 (
    0 => Array &1 (
        '#type' => 'html_tag'
        '#tag' => 'link'
        '#attributes' => Array &2 (
            'rel' => 'stylesheet'
            'media' => 'all'
            'href' => 'file_url_transform_relative:file_create_url:public://css/file-all?0'
        )
        '#browsers' => Array &3 ()
    )
) is identical to Array &0 (
    0 => Array &1 (
        '#type' => 'html_tag'
        '#tag' => 'link'
        '#attributes' => Array &2 (
            'rel' => 'stylesheet'
            'media' => 'all'
            'href' => 'file_url_transform_relative:file_create_url:public://css/file-all?0'
            'integrity' => 'sha384-psK1OYPAYjYUhtDYW+Pj2yc'
            'crossorigin' => 'anonymous'
            'random-attribute' => 'test'
        )
        '#browsers' => Array &3 ()
    )
).

/var/www/html/web/core/tests/Drupal/Tests/Core/Asset/CssCollectionRendererUnitTest.php:283
NickDickinsonWilde’s picture

ah yes, of course... sorry, actually do:

if (empty($css_asset['preprocessed']) && !empty($css_asset['attributes'])) {

using the other form wouldn't work for all cases.

LittleCoding’s picture

LittleCoding’s picture

Well that last patch did not work (href vs src). and an extra code comment.

LittleCoding’s picture

gnuget’s picture

Status: Needs review » Needs work

Hi LittleCoding

Thanks for your help but your patch does not contain the test added on #39 so even if it pass it will need work.

Also you should add interdiffs so we can know what was changed.

gnuget’s picture

Also, I'm curious why did you change the approach taken since #37? it seems that it was almost ready.

LittleCoding’s picture

Status: Needs work » Needs review

@gnuget The change in coding is to match the method used in the JsCollectionRenderer class, we should not have each doing their own thing.

Great point about keeping the testing. would you like to redo the patch with that included?

LittleCoding’s picture

Status: Needs review » Needs work

sorry did not mean to do that status change

gnuget’s picture

@gnuget The change in coding is to match the method used in the JsCollectionRenderer class, we should not have each doing their own thing.

Makes sense, thanks.

Great point about keeping the testing. would you like to redo the patch with that included?

I can do it later today/this week, feel free to do it if you have time before than me. :-)

+          } else {
+            // Prevent attributes from being added to an aggregate file.
+            $css_asset['attributes'] = [];
           }

In which case this else is going to be executed? it is possible to add custom attributes to the aggregated links generated by drupal?

Also, this else is not present in the JsCollectionRenderer either, so not so sure if I'm missing something here.

gnuget’s picture

Ok, new patch using the code of LittleCoding (using the same approach as JsCollectionRenderer) and re-adding the tests that I added in #39.

Also, @LittleCoding, I removed your else because I didn't found a way to add custom attributes to the aggregated files, if there is a way just let me know and I'm going to be happy to restore that change (and add code to test this part of the code).

markhalliwell’s picture

Status: Needs review » Needs work

Question: Why use array_replace instead of +=? += is used in the equivalent functionality for JS, and is a tiny bit better performance (given caching that's not really a major concern and it is a minor performance difference I believe)

Well, according to #2623960: Improve the merge() method on Attribute objects, this should really be using array_merge_recursive(). As far as "why", it's so that it can override any already set attributes that core may have made on the assumption of the file in question; more control.

Using += actually means they can't be overridden as it will simply ignore any keys that already exist in the array.

+++ b/core/lib/Drupal/Core/Asset/CssCollectionRenderer.php
@@ -61,6 +61,9 @@ public function render(array $css_assets) {
+          } else {
+            // Prevent attributes from being added to an aggregate file.
+            $css_asset['attributes'] = [];
           }

I don't feel like this is the best approach.

An asset could be external and not preprocessed, but still need attributes.

gnuget’s picture

Status: Needs work » Needs review

I don't feel like this is the best approach.

An asset could be external and not preprocessed, but still need attributes.

Actually if you see my patch, I didn't add that else :-p

Well, according to #2623960: Add a merge() method on Attribute objects, this should really be using array_merge_recursive(). As far as "why", it's so that it can override any already set attributes that core may have made on the assumption of the file in question; more control.

This might be worth it to create a follow-up to change JsCollectionRenderer as well?

LittleCoding’s picture

Status: Needs review » Needs work

gnuget if there is already something in the system that prevents the attributes value from being set for a aggregate file then yes, there is no need for the else that clears out any existing value.

And updating the method to of coding to using array_merge_recursive() for both CssCollectionRenderer and JsCollectionRenderer would be best as a follow-up issue, if that is the method to use. These two collection renderers not being in alignment is the starting part of this issue.

gnuget’s picture

I noted that you changed again the issue to needs work.

What needs to be addressed to changed it to needs review again?

LittleCoding’s picture

Status: Needs work » Needs review

Sorry. there is something odd going on with my browser and this form I guess. Thank you being on top of that!

LittleCoding’s picture

And jumping back to #55 #56 the if/else is within the switch so it would only clear the $css_asset['attributes'] for a $css_asset['type'] of file that also has a $css_asset['preprocessed'] value set.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

LittleCoding’s picture

Status: Needs review » Reviewed & tested by the community

The patch is working great.

gnuget good call on removing the ELSE from my patch from #48. I had confused "preprocess" with "preprocessed" so it was not doing what I had intended.

"preprocess" being the flag to excluded the library file from aggregation and "preprocessed" being the flag that indicated a aggregated group of files (rather then a single library file).

Looks like we are back to needed someone who is able to approve this to be added to core!

sandeep_jangra’s picture

The patch #48 working great. I just only removed the else part. Now i am adding updated patch here.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests, +Needs change record, +Needs issue summary update
+++ b/core/lib/Drupal/Core/Asset/CssCollectionRenderer.php
@@ -72,6 +75,11 @@ public function render(array $css_assets) {
 
+      // Attributes may only be set if the stylesheet is outputed independently.
+      if (!empty($element['#attributes']['href']) && !empty($css_asset['attributes'])) {

Should be 'output' here.

I think we need test coverage for this.

gnuget’s picture

Does this needs a change record even if in the official documentation says that this should already work?

I think that this is why this is marked as a bug and not as a feature request.

https://www.drupal.org/docs/8/theming/adding-stylesheets-css-and-javascr...

LittleCoding’s picture

Great point!, Better to have the information available.

renguer0’s picture

Issue summary: View changes
LittleCoding’s picture

Issue summary: View changes

restore issue summary

LittleCoding’s picture

Updated the comment as per comment #64 and fixed test patch to apply in 8.9.x. The patch was failing to apply due to a changes in testing.

LittleCoding’s picture

Only list patch for current development branch (8.9.x). change status to Need Review.

just a reminder:

  • The patch in comment #14 works for 8.2.x to 8.6.x
  • The patch in comment #54 works for 8.7.x
  • The patch in comment #69 works for 8.8.x to 8.9.x
gnuget’s picture

This looks pretty good, thanks LittleCoding.

I'm going to remove a few tags:

  • API change: Given that the documentation explicitly says that this should already work on this way there is not an API change so no need to have this tag.
  • Needs tests: Since #39 the patch has a test that covers this.
  • Needs change record: Given that the documentation explicitly says that this should already work on this way no
    change record needed..
  • Needs issue summary update: This was updated at #68.

This seems ready to me. Thanks!

Status: Needs review » Needs work
jungle’s picture

Fixing test failure in #69. Adjusted the indexes in the data provider as index 1 is missing.

jungle’s picture

Refactored tests by using Prophecy.

 /**
   * {@inheritdoc}
   */
  protected function setUp(): void {

Added missing comment and added return type hinting which is mandatory in Drupal 9 now.

LittleCoding’s picture

Thank you @gnuget cleaning up the tags and pulling all those question together with answers,

@jungle thank you for fixing the testing and for thinking ahead to D9. The patch from comment #74 is working for me.

how about a few more testers to confirm this.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

LittleCoding’s picture

Looks like we need a third version of this patch to work with D9+

gapple’s picture

LittleCoding’s picture

Status: Needs review » Reviewed & tested by the community
LittleCoding’s picture

alexpott’s picture

Crediting people from the duplicate issue.

alexpott’s picture

Title: Allow attributes passed with CSS in libraries (SRI) » [backport] Allow attributes passed with CSS in libraries (SRI)
Version: 9.1.x-dev » 8.9.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed and pushed 758adf0e97 to 9.1.x and ea20f12a26 to 9.0.x. Thanks!

We need a backport patch. I think that'd be #74 without the protected function setUp(): void {

  • alexpott committed 758adf0 on 9.1.x
    Issue #2716115 by LittleCoding, gnuget, jungle, gapple, tatarbj,...

  • alexpott committed ea20f12 on 9.0.x
    Issue #2716115 by LittleCoding, gnuget, jungle, gapple, tatarbj,...
mrinalini9’s picture

Assigned: Unassigned » mrinalini9
mrinalini9’s picture

Assigned: mrinalini9 » Unassigned
Status: Patch (to be ported) » Needs review
FileSize
5.34 KB

Updated patch #74, without the protected function setUp(): void { as mentioned in #83, please review.

gnuget’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
691 bytes

Hi @mrinalini9

Thanks for porting the patch!

Whenever a new patch is provided it is recommended to provide an interdiff so we can see what changed between patches.

You can read here how make interdiffs.

For this time I made it for you, It is attached.

And looks great!

Thanks again.

alexpott’s picture

Discussed with @lauriii we decided to hold off on committing this to 8.9.x until after 8.9.0 because of the possibility of existing sites breaking because of incorrect integrity hashes in css component definitions. The plan is to commit this early in 8.9.1 dev cycle to give sites that have incorrect libraries the best chance of discovering this before release. Note that Drupal 9.0 will support this so if you 100% needs this to work you can apply the 8.x patch or move to Drupal 9 :)

To this end I've created the change record https://www.drupal.org/node/3144708

alexpott’s picture

Issue tags: +Security improvements

This is definitely a security improvment.

gapple’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
669 bytes
5.25 KB

As in #78, the condition can be simplified since the element is always a link element with an href attribute since 8.7

LittleCoding’s picture

tested both the 8.9-dev (#91) and 9.1-dev (#78) and they are working as expected.

LittleCoding’s picture

Status: Needs review » Reviewed & tested by the community

The was a mix-up and this has not been committed in the 8.9.x branch yet. So the change record made by @alexpott has an edit added to reflect this.

I have again reviewed and tested patch #91 to confirm it is still working and updated status. Who can this be assigned to to be committed to the current 8.9.x so it will be included as part of 8.9.4 and this issue can be completely closed?

ksemihin’s picture

The same version as in comment #91
But with replacing
From

$element['#attributes'] += $css_asset['attributes'];

To

$element['#attributes'] = NestedArray::mergeDeep($element['#attributes'], $css_asset['attributes']);

What allows to override type and other default attributes.

gapple’s picture

What's the reason for wanting to be able to change those default attributes? Is there not currently a way to change those attributes when necessary?

Since the current patch is backporting changes from D9 to D8.9, this change should probably be a separate issue so that it's committed to the latest release first. Since this same behaviour is done separately for JS and CSS, a patch should address both renderr classes for consistency.

larowlan’s picture

Status: Reviewed & tested by the community » Needs review

There seems to be some conjecture between #91 and #95 here - can we make sure the latest patch is the one under consideration? Thanks in advance

LittleCoding’s picture

Status: Needs review » Reviewed & tested by the community

Agreed that given this is at the point of being a backport and that the patch in #95 alters the feature, it should be made into a new issue in the queue.

LittleCoding’s picture

This was not committed as part of ether of last week's core updates (8.9.4 or 8.9.5). So again there is an edit to the Change Record https://www.drupal.org/node/3144708

Nug, nug. Poke, poke. to anyone following this that can get this backport committed for this to be finished.

LittleCoding’s picture

Updated Change Record to 8.9.7 as this was not included in 8.9.6

KarimBou’s picture

Hey,
The attribute now works and i'm able to add a title for instance.
changing default rel="stylesheet" to rel="alternate stylesheet" doesn't seem to work"

"libraries.yml":

css:
    theme:
      style/style.css: {}
      style/print.css: { media: print }
      style/styleguide.css: { type: "css", attributes: { rel: "stylesheet alternate ", title: "alternate-css" } }

Any idea why ?

LittleCoding’s picture

Hey KarimBou,

This does not allow override of the basic required attributes (href, media, rel). Rather it allows the inclusion of additional attributes like: integrity, title, crossorigin, disable, etc.

If you look over ksemihin patch from #94, that will allow the override of those default attributes. I don't know if ksemihin did start a new issue to have that feature option.

LittleCoding’s picture

Updated Change Record to 8.9.8 as this was not included in 8.9.7

KarimBou’s picture

@LittleCoding Thanks for pointing that up, went past that one :)
I do think it's relevant to allow that out of the box.

alexpott’s picture

Re-upping #91 since that is the patch that should be considered for backport. #95 offers a different feature set and would need to be changed against 9.2.x first.

alexpott’s picture

Title: [backport] Allow attributes passed with CSS in libraries (SRI) » Allow attributes passed with CSS in libraries (SRI)
Status: Reviewed & tested by the community » Fixed

Discussed with @catch and we agreed to backport this to 8.9.x

Committed e880e48 and pushed to 8.9.x. Thanks!

  • alexpott committed e880e48 on 8.9.x
    Issue #2716115 by LittleCoding, gapple, gnuget, jungle, alexpott,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

KarimBou’s picture

Hello everyone, any chance we could see a compatibility of this on Drupal 9.3.x ?

gapple’s picture

@KarimBou The change to allow additional attributes according to the original scope of this issue was committed to 8.9+, and 9.1+

If you're interested in overriding default attributes as in #94, you should open a separate issue as previously mentioned.