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
See: #2489460: [Meta] Move module.theme.css files to Classy or Seven
Proposed resolution
Move the CSS file to classy.
Create a library for the CSS file.
Add the library in the Classy twig template.
Remaining tasks
- Figure out what we're allowed to move (see comments).
- Move all the things.
User interface changes
None for Classy. Stark will be more stark.
API changes
None
Comment | File | Size | Author |
---|---|---|---|
#68 | VOLTRON_1.jpg | 236.85 KB | star-szr |
#66 | move_user_theme_css_to-2489564-66-after.png | 93.45 KB | star-szr |
#66 | move_user_theme_css_to-2489564-66-before.png | 94.54 KB | star-szr |
#66 | move_user_theme_css_to-2489564-66-diff.png | 399.73 KB | star-szr |
#65 | move_user_theme_css_to-2489564-65.patch | 2.17 KB | gints.erglis |
Comments
Comment #1
jstollerThis module brings up a number of issues that need to be addressed before any CSS can be moved. The module contains two *theme.css files: user.icons.theme.css and user.theme.css. The former contains references to background images for the user icon in the toolbar. The later contains styling for the user password editing form. Neither of these correlate to templates in Classy, so while they're undeniably front-end styling there is some question as to what we should do with them.
Based on discussions at DrupalCon, it sounds like the anything dealing with the toolbar should be classified as "admin" and therefore not moved to Classy. If this is the case then perhaps we need to rename user.icons.theme.css to user.icons.admin.css?
The user.theme.css file is a bit more complicated. The markup being targeted by this CSS appears to be coming from user.js, rather than from a template file. Plus, there is already a user.admin.css file and the user.module.css file contains basic styling so the password strength indicator will work. The user.theme.css file just improves that with nicer styling. So, we're a little stuck on how to proceed. Do we need to modify things so the password form has a template file? Can we move user.theme.css to Classy without that template? Can we move user.theme.css to Classy at all, or are we deeming it "admin" and thus unmovable? If it is "admin," then should the CSS be merged into user.admin.css and/or user.module.css, or do we now want to maintain the M.A.T. (module/admin/theme) concept, so we can keep display styling separate from functional styling in cases like this?
Comment #2
LewisNymanYes
I think if the styling is clearly theme styling, like the colours for the password indicator, then we should move it to Classy. We can create a followup issue to decide what to do about classes added in Javascript, in this situation it feels like the module is just completing the UI component by adding state classes. I think it's not that bad to have them added in the module but not styled in the module for the time being.
Comment #3
jstollerCurrently user.theme.css is part of a larger library that's called in user_form_process_password_confirm(), which is the form element process handler for client-side password validation. There's no template to attach it to, so if we remove user.theme.css from the drupal.user library then how do we load the file?
It seems like the cleanest way would be to implement #2451411: Add libraries-override to themes' *.info.yml. Then Classy could just add another CSS file to the existing library provided by the User module. There is the hook_library_info_alter() function, but I'm not sure if it would work for this and it certainly seems really hackie.
Thoughts?
Comment #4
lauriii@jstoller: I'm not sure if there is going to be working solution for that other issue anytime soon. As far as I've seen It's quite complicated and there is no work done yet to make it happen. Seven has similar PHP code in seven.theme file so I think it could be copied and used for this purpose. Not optimal but probably necessary at this point.
Comment #5
jstollerHere's a first stab at this, applying the approach used in Seven to add a theme CSS file to an existing core library, but it doesn't seem to be working. The CSS is not loading. Anyone have any idea why?
Comment #6
star-szr@jstoller thanks for all this. It seems to me like the code is working as expected. I'm fairly sure you need to clear caches because the library definitions are cached.
Comment #7
jstollerHmmm. It could be some other issue with my dev environment, but I know I cleared the cache several times from the performance page. If I went to the add user page, the password strength meter was long and grey after my changes, when it should be shorter and colored.
Comment #8
lauriiiSeems to work for me too!
@jstoller: could you check that your git configuration aligns with the standards that are given here: https://www.drupal.org/documentation/git/configure
Comment #9
jstoller@lauriii, do you see a problem with my patch file, or do you think my git config could be somehow causing the code not to work on my system?
Comment #10
lauriiiThe problem is on the patch. With the git config that we suggest to use the patch will became a little bit easier to read :)
Comment #11
mortendk CreditAttribution: mortendk commentedreroll
Comment #12
mortendk CreditAttribution: mortendk commentedlets try this time with a file
Comment #13
mortendk CreditAttribution: mortendk commentedscreenshots:
seven:
classy:
Bartik:
Comment #14
sqndr CreditAttribution: sqndr as a volunteer commentedI created a comment to discuss the naming convention for the libraries. I don't feel like we should add the
classy.
before user, since you're now includingclassy/classy.user
which seems pointless to me. I suggestuser.theme
, but let's discuss that in the comment.Comment #15
jstoller@sqndr: See my comment on the other issue. While we include "classy/" when adding the dependency, it does not seem that drupal includes that in the way it lists its libraries. If library names must be unique, as it appears they must, then we should namespace them with the theme name.
Comment #16
jstollerSeems I was wrong and we don't need to worry about name conflicts in hook_library_info_alter(), as long as we add in the missing
$module
parameter. Apparently Seven is doing this incorrectly as well. The attached patch fixes the hook implementation and removes the unnecessary prefix from the library name.Comment #17
star-szrPlease use $extension, I created #2490936: hook_library_info_alter() docs and function signature are slightly out of date to fix the docs :)
Other than that, looks good!
Comment #18
jstollerLast time I trust that Drupal documentation. ;-)
Here's an updated patch with $module changed to $extension.
Comment #19
star-szrI missed this before, but the patch no longer deletes the user.theme.css from user module ;) #mortendkblaming
Comment #20
mortendk CreditAttribution: mortendk as a volunteer commentedassign blame !
rerolled the patch
Comment #21
mortendk CreditAttribution: mortendk as a volunteer commentedlets try this again and also rename icons.theme
Comment #22
mortendk CreditAttribution: mortendk as a volunteer commentedAs a follow up as its out of scope (so DONT add it to this patch)
We should look at what to do with the user.admin.css its all css that should live over in seven #2491149: move user.admin.css into seven where it belongs
Comment #23
star-szrThere's something going on where visually it looks slightly different in Seven (less spacing underneath the Password strength), I did notice that the Classy CSS is being loaded at the very end which seems wrong.
Does this have anything to do with #2474091: CSS files from themes are being loaded in reversed order or are we just using it wrong somehow?
Before:
With #21:
Comment #24
mortendk CreditAttribution: mortendk as a volunteer commentedTheres defently something going on with the cascade - THIS IS WHAT HAPPENS WHEN YOU WRITE SHITTY CSS PEOPLE
... ok time to debug ;)
Comment #25
star-szrEven moving the user.theme.css added to Classy in this patch to the 'component' group which is used in most of Seven puts it below all the Seven stuff. Why is that?
Comment #26
star-szrAnd I don't want to derail this issue too much but shouldn't making a library containing a CSS file from core depend on a library containing a CSS file from Classy make the Classy CSS come first? Certainly I would expect/hope JS dependencies to work that way anyway…
Comment #27
mortendk CreditAttribution: mortendk as a volunteer commentedI have no idea classy should be loaded first right
order:
Comment #28
star-szrI would think so. Going to try and find some time tonight to poke around at the libraries stuff and see how it's designed.
Comment #29
sqndr CreditAttribution: sqndr as a volunteer commented@Cottser: See #2389203-57: Validate the CSS categories in libraries.yml files.
Comment #30
sqndr CreditAttribution: sqndr as a volunteer commentedCSS Loading order
Kitten is a sub theme that has
base theme: classy
.The loading order will be
Base styles from Classy, then from Seven/Bartik/Kitten. (Weight: -200)
Layout from Classy, then from Seven/Bartik/Kitten. (Weight: -100)
Component from Classy, then from Seven/Bartik/Kitten. (Weight: 0)
State from Classy, then from Seven/Bartik/Kitten. (Weight: 100)
Theme from Classy, then from Seven/Bartik/Kitten. (Weight: 200)
Needs documentation?
I don't know where the documentation for this is. If there is none, I'll improve the example above and add it to the docs.
Comment #31
sqndr CreditAttribution: sqndr as a volunteer commentedclassy.libraries.yml
Like this it works. Maybe it's because it's done in an alter hook? Good luck digging into the code Cottser!
Comment #32
jstollerI think part of the problem here is that module CSS wasn't built with SMACSS in mind. It was divided by module/admin/theme. Now that we're moving that "theme" CSS over, we'll need to split it up according to SMACCS if we want to be able to load it correctly. Most of the CSS in Seven and Bartik is loaded as components, so we can't just throw everything in "theme" if we want it to load before the sub-themes.
Comment #33
star-szrThis may have something to do with Seven's global-styling library being loaded on every page, therefore it comes first. Any library that isn't loaded on every page seems to come after, possibly because of Drupal\Core\Asset\AssetResolver::sort() - see the inline comments there:
So this kinda works as expected:
But I don't think we want to do that…
It sure would be nice to avoid having to refactor all this CSS as we move it!
Comment #34
jstollerIt seems to me that we should let Classy do whatever makes sense for Classy and let Seven react to that in a follow-up issue. It's the sub-theme's job to adjust to the base theme, not the other way around.
Comment #35
star-szrWe can't make D8 less shippable.
Comment #36
jstollerI think this depends on your definition of shippable. I'd take CSS in Classy over a small spacing issue in Seven. :-)
Does this mean we need to move away from our current approach and do a larger patch refactoring CSS for all the themes? Do we just add a complementary CSS file to Seven to patch the spacing issue for now?
Comment #37
star-szrI didn't mean that this particular issue would do that, but that likely others will have larger repercussions.
Comment #38
mortendk CreditAttribution: mortendk as a volunteer commentedisnt the issue here basically that we then have to correct the css in seven so we dont have visual regression ?
then we can move forward ?
Comment #39
star-szrYeah for this particular issue that seems right. !important everything! :P
Comment #40
star-szrWe talked about this on the Twig call, it's not just a Seven issue. It will be an issue to anyone using Classy as a base theme. We talked about when you declare a CSS dependency it should (within the SMACSS group) alter the source order of the CSS, similar to how the JS dependencies work. We also talked about potentially re-purposing #2474091: CSS files from themes are being loaded in reversed order to discuss this ordering/dependency issue.
All of this should probably be on the parent issue but it's kinda too late. Sorry :(
Comment #41
star-szrAnother IRC discussion, getting pretty in depth so time to move this higher level discussion to the parent. In the meantime someone could try to implement what I've proposed as a straight hook_library_info_alter(), that would probably be a good POC.
#2489460-20: [Meta] Move module.theme.css files to Classy or Seven
Comment #42
almaudoh CreditAttribution: almaudoh commented@lauriii #4: All hope is not yet lost :) I've started some work over at #2451411: Add libraries-override to themes' *.info.yml. There's a patch now for testing and review.
On dependencies, AFAICS there is nothing in the codebase that points to ordering by SMACSS groupings, I could be wrong, so this actually needs some discussion.
Comment #43
mortendk CreditAttribution: mortendk as a volunteer commented@cottser do we need a meta where we actually discuss how the css ordering should be ?
- this is gonna be an epic pita further down the road if we dont figure it out ... asap ;)
Comment #44
star-szrI think the dependencies ordering deserves discussion but won't solve this.
What we want here is to append to libraries, not make them depend on classy. In their words we want core > classy > seven, not classy > core > seven.
Comment #45
mortendk CreditAttribution: mortendk as a volunteer commented+1 cottser
Comment #46
star-szrGoing to try and put a patch up here, in the meantime here's a reroll of #21.
Comment #47
star-szrSince this one actually needs something like #2497667: Add libraries-extend to themes' *.info.yml and there's not even a patch there yet, here's a patch that should do the trick.
This gets the library definition from classy.libraries.yml and adds it to the 'theme' group in the existing user/drupal.user library, "extending it". Unfortunately getLibraryByName() returns the numeric constant for the group so I think 'theme' needs to be hardcoded.
The screenshot below shows 8.0.x vs. this patch, and that the CSS file is staying in the exact same spot:
Edited to link screenshot.
Comment #48
LewisNymanI can confirm this works as expected. The only thing we need to do it change the file to css/components/user.css
Comment #49
LewisNymanComment #50
star-szrSure, here's that change.
Comment #51
LewisNymanGreat, thanks.
Comment #52
davidhernandezSame question that I've had on other issues. Should this go in a "theme" folder, since it is in the "theme" category.
Comment #53
RainbowArrayI think ideally we'll do a follow-up to clean up any inconsistencies if and when these all get in. We're essentially using the theme and component library groups as a way to keep the file loading order the same rather than a meaningful description of what those library elements do. It would be great to straighten that out, but I think it's more important to get the move done first.
Comment #54
davidhernandezWe're only going to get one chance at this, unfortunately. We're too close to RC to rely on follow ups.
Classy can have things added to it later, but I don't believe we'll be able to modify what is there come RC and release, so lets make sure we're getting it right when it first goes in.
I agree it should stay in the theme category. I'm suggesting the files go in a theme folder, instead of the components folder where the files that are added in the component category are stored.
Comment #55
LewisNymanThis is a component so we need to change this.
Comment #56
star-szrThat will mess with the loading order, won't it? This might be an exceptional case.
Comment #57
LewisNyman@Cottser It will change the loading order. We've found in other issues like #2553461: Move system icons.theme.css, messages.theme.css, and progress.theme.css to Classy, this should ensure that it's loaded as early as possible.
Comment #58
rudraram CreditAttribution: rudraram at Axelerant commented@LewisNyman So
{ weight: -10 }
ensures the early loading of the file?Comment #59
davidhernandezThe loading order is a combination of category (component, theme, etc) and weight. Also factored in is whether it is added from a module or theme. Files added from modules appear higher. All the "component" files added by themes are somewhat grouped together. The weight will make it get added higher in that grouping.
Comment #60
RainbowArrayComment #61
LewisNymanGreat, thank you.
Comment #62
star-szrThe hook library alter is putting it in the theme group, not component group.
Comment #63
LewisNyman@Cottser Good catch. Here's an updated patch
Comment #64
gints.erglis CreditAttribution: gints.erglis commentedpatch does not apply
Comment #65
gints.erglis CreditAttribution: gints.erglis commentedComment #66
star-szrThe CSS does get moved up but there are no visual regressions, markup diff screenshot and before/after screenshots below. Back to RTBC, thanks!
Before:
After:
Comment #67
LewisNymanThanks! Setting to postponed for #2566771: [Voltron patch] Move all remaining *.theme.css to Classy
Comment #68
star-szrComment #69
akalata CreditAttribution: akalata commented