Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Let's add a new library definition:
drupal.node:
version: VERSION
css:
css/node.module.css: {}
... this looks reasonable, but in fact this is broken. You need to specify a category:
drupal.node:
version: VERSION
css:
layout:
css/node.module.css: {}
This is far from being obvious.
Beta phase evaluation
Issue category | Task because Drupal needs to validate a requirement that isn't obvious. |
---|---|
Issue priority | Normal because this change adds test coverage for libraries.yml |
Unfrozen changes | Unfrozen because it only changes tests. |
Proposed resolution
Add some validation code in LibraryParser.php
to ensure the "category" is right.
In case you use a path as category, throw an exception like:
You have to key the css files in the "$module.library.yml" file by category (css/node.module.css seems is a filename, allowed categories are ...)
In case someone actually uses a category, throw an exception like:
You use an invalid category "invalid_cateogry" in "module.libraries.yml". Allowed categories are ...
An example for such a library.yml file would be:
drupal.node:
version: VERSION
css:
invalid_category:
css/node.module.css: {}
Remaining tasks
Fix tests that are not using categories.
User interface changes
API changes
Comment | File | Size | Author |
---|---|---|---|
#90 | validate_the_css-2389203-90.patch | 7.86 KB | hussainweb |
#87 | interdiff-2389203-81-86.txt | 1.86 KB | lokapujya |
#86 | validate-the-CSS-categories-in-libraries.yml-files-2389203-86.patch | 7.91 KB | dcmul |
#22 | 2389203-22.patch | 4.57 KB | lokapujya |
#22 | interdiff-2389203-21.txt | 560 bytes | lokapujya |
Comments
Comment #1
Wim LeersComment #2
zealfire CreditAttribution: zealfire commentedI am not sure whether i am going in right direction so writting a pseudo before actually submitting a patch:
if ($type == 'css' && !empty($library[$type])) {
foreach ($library[$type] as $category => $files) {
+if(empty($file)){
+ throw exception 1 as above
+}
+if(!empty($category) && $category not in valid categories ) {
+ throw exception 2 as above
+}
foreach ($files as $source => $options) {
if (!isset($options['weight'])) {
$options['weight'] = 0;
}
Please tell the correct way.
Thanks
Comment #3
Wim LeersYes, something like that is what we need. There's no need to feel afraid to post a patch, even if it's imperfect. We all post imperfect patches all the time — we all rely on the feedback from others will help get us there :)
Comment #4
naiduharish CreditAttribution: naiduharish commentedWorking on it
Comment #5
Wim LeersThanks for working on this! Ping me on IRC if you have questions :)
Comment #6
naiduharish CreditAttribution: naiduharish commentedI have made changes in LibraryDiscoveryparser.php and added the validations for invalid category.
Comment #7
naiduharish CreditAttribution: naiduharish commentedComment #8
hussainwebNitpick: Please try to alphabetize the list.
Putting single quotes around %s in '%s'.library.yml might be confusing. On that note, I think the filename will be .libraries.yml.
Nitpick: There should be a space after //.
Remove the extra whitespace.
Comment #10
naiduharish CreditAttribution: naiduharish commentedHi @hussaian,
Thanks for the feedback. I have corrected the issues mentioned above and resubmitting the patch.
Comment #11
naiduharish CreditAttribution: naiduharish commentedComment #13
lokapujyaDefine the exceptions.
Comment #15
lokapujyaFixed.
Comment #17
lokapujyaComment #18
lokapujyaWe discovered 2 tests (including one that dynamically adds a library) that doesn't provide a category.
Comment #19
hussainwebIt seems we were working on this at the same time. Anyway, I applied my remaining changes on top of your patch. Basically, I renamed the exception class to something slightly smaller and reformatted a block of code. Interdiff is attached.
Comment #20
lokapujyaLooks good to me. RTBC.
Comment #21
gvsoI wonder why the test has the extension .js
Comment #22
lokapujyaWe could have kept it, since it shows that the file extension is not required to be the same as the category. But, I think it's just from a copy & paste.
Comment #23
dawehnerIt is great to see that someone has picked this one up!
Note: Someone could write an additional test in LibraryDiscoveryParserTest to have that bit of extra coverage available.
Not 100% sure whether we need 2 new exceptions here ... but well I don't have a strong opinion.
Nitpick: Always use 1 space after the "//".
Comment #24
hussainwebIt looks like the exception I renamed in #19 was not carried forward in #22. Any particular reason? There was no explanation in that comment.
I am not too comfortable with two exceptions either and think there might be a way to put it under an umbrella. They indicate two different things but they are very much related.
On first thought, I would think we could just use
InvalidCssDefinitionException
as that could indicate both a missing category and an invalid category. Thoughts?Comment #25
hussainwebFor #24.
Comment #26
lokapujya" Any particular reason?" My mistake.
Comment #27
hussainwebThat's okay. Did you have a thought on reducing it to one new exception as discussed in #23.1 and #24?
Comment #28
lokapujyaI think what you said in #24 is ok. The error message will still be different though, right? So, I think that works.
Comment #29
hussainwebMade the changes as discussed. Both comments in #23 are addressed.
Comment #30
lokapujyaThe new exception would need to be added to the throwing function's comment.
Would we actually want to catch this exception? It's too bad that the page will show a generic error if there is an exception.
Comment #31
lokapujyaComment the throw.
Comment #33
Wim LeersLooking good, but we still want a unit test for this.
Comment #35
Wim LeersSpecifically, we want a test for that in
\Drupal\Tests\Core\Asset\LibraryDiscoveryParserTest
. There are many examples to start from in that one already, so should be doable :)Comment #36
lokapujyaAdded Unit Tests.
Comment #37
Wim LeersAwesome!
Almost there :)
This message is pretty vague.
Let's turn it into: "Every CSS asset must be nested under a SMACSS category, i.e. either theme,
or base. See for details."
Let's similarly expand this message.
Comment #38
lokapujyaWe already say the categories in the exception. The @expectedExceptionMessage is only searching for a substring of the actual exception. I do like your wording better than saying "You have...". On the other hand, the way that the exception message currently reads, it tells you which file was wrong.
Also, do we want to catch this exception anywhere or catch it in a new issue?
Comment #39
suntog CreditAttribution: suntog commentedMade advised edits comments as per #37
Comment #41
hussainwebFixing messages in #39 and added two more.
Comment #42
wheatpenny CreditAttribution: wheatpenny commentedComment #43
wheatpenny CreditAttribution: wheatpenny commentedNovice task: review the patch in #41 as outlined in this contributor task: https://www.drupal.org/contributor-tasks/review
Comment #44
wheatpenny CreditAttribution: wheatpenny commentedAdjusting positioning and content of beta review based on feedback by YesCT.
Comment #45
sqndr CreditAttribution: sqndr as a volunteer commentedI applied the patch from #41 on a clean installation and I get:
Update: It also breaks using Simplytest.me
Comment #46
sqndr CreditAttribution: sqndr as a volunteer commentedJust to make sure, when we say "validate the css categories", we only mean that people should group their css, right? We're not forcing them to use SMACSS naming, right? If company's have a different css architecture, they can still do:
That's correct, right?
Let's say I'm using a third-party library (or a css framework) that I want to include. I'll always have to insert the css into a certain category … which might feel kinda strange. Let's say I'm using the Bootstrap library. If I were to use that framework, it contains css from all SMACSS categories. That means I'd do something like:
Comment #47
lokapujyaI think this patch throws an exception if you don't use one of the SMACSS categories. We should make sure that's really what we want.
Comment #48
sqndr CreditAttribution: sqndr as a volunteer commentedI think this patch throws an exception if you don't use one of the SMACSS categories. We should make sure that's really what we want.
Discussed this with Lewis Nyman yesterday and that is not what we want, especially since the official SMACSS terminology is modules while we ofter say components and theme while we often use skin, due to the fact that modules and themes have a specific meaning in Drupal.
People should also not be forced to use SMACSS categories. I think it started from Yahoo and it has proven to be very successful, but if people want to organise their css in a different way and/or with a different naming convention, they should be able to do so.
Comment #49
Wim LeersNo, that's not correct, we are forcing SMACSS. Discussing that here is out of scope.
Comment #50
lokapujyaRegarding #45:
$library['css'] has a category that is empty that is causing the exception.
Comment #51
LewisNymanI don't understand, where are we defining the list of valid categories? Even if we are only implementing the enforcement here, this is a TX regression.
Comment #52
lokapujyaAllowing a category to be empty and adding a test.
Comment #54
lokapujyaComment #55
lokapujyaReminder, fix the spacing on this line in next patch.
Comment #56
sqndr CreditAttribution: sqndr as a volunteer commentedAs Wim pointed out:
We currently agreed on forcing SMACSS, apparently. Can we open an issue to discuss this change (#52), before we start working on this? The SMACSS categories also define the order the css is loaded in. If you don't categorise the css, what will be the weight of the css? When will be loaded?
Can we keep this issue to forcing the SMACSS categories? If we want to move into another direction, make sure to open a discussion first - and than work on the patch. If I'm wrong, and we agreed somewhere to STOP using SMACSS, please refer to the issue where this has been agreed upon.
Comment #57
Wim LeersIn
common.inc
:+ in
LibraryDiscoveryParser
:Exactly.
Let's rename this to
InvalidCssCategoryException
.All of this can be replaced by:
Comment #58
lokapujyaI'm not sure why it can be replaced.
seven_library_info_alter() is unsetting the file name, but leaving the category. This part prevents an exception by skipping over the following code.
This part throws a specific exception message, if a file is not under a category.
Comment #59
Wim LeersOk, I figured we don't care about validating invalid categories if they don't contain anything. You want to still validate those. That's fine (better).
But then let's make the code easier to understand. Let's change what you currently have:
to?
Comment #60
lokapujyaSure, and added a comment.
Comment #61
lokapujyatest it.
Comment #62
Wim LeersThis nested if-test is always going to be true. No need to repeat it.
Comment #63
pjbaertI processed the remark mentioned in #62.
Comment #64
pjbaertLet's change this status to needs review
Comment #65
Wim LeersNit: the first line wins, the second line can be removed. Can be done upon commit.
Comment #66
alexpottAre we not missing a test file for this?
Comment #67
deepakaryan1988Removing sprint weekend tag!!
As suggested by @YesCT
Comment #68
Wim LeersComment #69
deepakaryan1988Sorry, these issues were actually worked on during the 2015 Global Sprint
Weekend https://groups.drupal.org/node/447258
Comment #70
deepakaryan1988Comment #71
deepakaryan1988Rerolling the patch #63 and also address comment #65
Comment #73
lokapujyaThis change looks good. The rest of the changes were maybe included by mistake.
Comment #74
deepakaryan1988I dont know why it's failing..
I am unable to debug!!
Can anyone help me with this?
Comment #75
lokapujyathose changes should not be part of the patch. Only the one change.
Comment #76
deepakaryan1988@lokapujya Ok thnx..
making that change only.
Comment #77
deepakaryan1988@lokapujya But these changes are in that patch.. :(
Still confused.
Comment #78
dcmul CreditAttribution: dcmul as a volunteer commented@deepakaryan1988 Noticed a syntax error.
$path = substr($path, strlen($this->root) 1);
Comment #79
lokapujyaCan you re-do the reroll of #63? The patch in #71 contains changes that should not be included. Only the change shown in #73 is correct.
Comment #80
lokapujyaComment #81
Sivaji_Ganesh_Jojodae CreditAttribution: Sivaji_Ganesh_Jojodae commented#63 re-rolled with change mentioned in #73.
Comment #83
Manjit.SinghComment #85
lokapujyaNeeds a test file for css_empty_category similar to css_missing_category.libraries.yml.
Comment #86
dcmul CreditAttribution: dcmul as a volunteer commentedAddress the issue raised in #66
Comment #87
lokapujyaIncluding the interdiff so that we can review the changes.
Comment #88
lokapujyaLooks like there is another test that has a duplicated @expectedExceptionMessage
Comment #89
Wim LeersComment #90
hussainwebRerolling and also fixing issue in #87. Since it is just a small line change, it didn't make sense to separate patches and create an interdiff. I hope it's okay.
Comment #91
lokapujyaThe whole purpose of the interdiff is so that we can see just the small change!
Comment #92
lokapujyaLooks good though.
Comment #93
joelpittetYay more tests! RTBC++
Comment #95
hussainwebI have seen this error in other issues as well. I think it is to do with a bot or random. Retesting.
Comment #97
LewisNymanBack to RTBC then
Comment #98
alexpottDo we really want limit the categories to SMACCS categories? Couldn't we just define a weight for unknown categories and say they come after theme? i.e the lowest of the low.
Comment #99
lokapujyaCurrently, SMACCS is already required. I think this issue is supposed to make it obvious that you did not specify a category.
One possible problem with this issue is that if the CSS is dynamically added without a category, then an exception can be thrown.
Comment #100
joelpittet@alexpott I'd prefer if we could put the non SMACSS category in like the example, and people questioned this in the theme talk we did tonight in Vancouver user group. We can by defining aribrary CSS_ contstants but that is not cool but better than nothing...
But like @lokapujya mentioned in #99 it's already required.
Comment #112
smustgrave CreditAttribution: smustgrave at Mobomo commentedComment #113
smustgrave CreditAttribution: smustgrave at Mobomo commentedBefore anyone spends time on this actually want ot make sure it's still a valid task.
Comment #115
smustgrave CreditAttribution: smustgrave at Mobomo commentedSince there hasn't been a follow up going to close out for now. If still a valid task please reopen.
Thanks all