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.
Follow up from #1982256: Clean up html.html.twig markup
Meta issue: #1980004: [meta] Creating Dream Markup
Problem/Motivation
The body's classes do not follow Drupal 8's CSS coding standards, we have classes that have never been used: .html
, strange empty classes (page-node-), etc.
html
front / not-front
not-logged-in / logged-in
in-maintenance
page-node
page-node-
page-node-$nid
...
Proposed resolution
Drupal will provide the following classes as default in <body>
:
.path-frontpage (if on the frontpage)
.path-$path (first part of the path - .path-admin, .path-node)
.user-logged-in
.page-maintenance (existing class to use instead of in-maintenance)
Remaining tasks
None
User interface changes
n/a
API changes
Markup/CSS class changes only.
Comment | File | Size | Author |
---|---|---|---|
#134 | change_the_body_class-2234331-134.patch | 8.67 KB | lauriii |
#134 | interdiff-2234331-129-134.txt | 2.12 KB | lauriii |
Comments
Comment #1
mortendk CreditAttribution: mortendk commentedComment #2
mortendk CreditAttribution: mortendk commentedComment #3
mortendk CreditAttribution: mortendk commentedComment #4
mortendk CreditAttribution: mortendk commentedComment #6
mortendk CreditAttribution: mortendk commentedComment #7
paolomainardi CreditAttribution: paolomainardi commentedI guess we should handle also taxonomy and user pages as well, what do you think ?
Comment #8
nicholasruunu CreditAttribution: nicholasruunu commentedThe sidebar classes can be quite handy to transform the layout based on using/not using the sidebar.
Something simpler than what we had could probably be used.
Also now we're losing the admin class which could also be handy while styling inline admin stuff.
Was there any other functionality with the class suggestions we're missing now?
Comment #9
mortendk CreditAttribution: mortendk commented@paolomainardi
i dont think that drupal out of core should handle all situations it probablly could end up with, themes have a preprocess function for a reason.
Lets follow the 90% idea, at not end up in endless "what-if"' ask your self: Do I always have a need for a class to identify a user page or the taxonomy page ? - or is that done in another place on the page.
a usecase could be to have a
.page--$first-path-of-the-url
that would give us out the box a path to different sections@nicholasruunu
Yup the sidebar is handy - That is if your theme is build like a 3 column site & the sidebars are called sidebar_first sidebar_last, if you dont then its just cruft. Bartik & Seven both have these build in, and they are defined in the .theme preprocess
I think is documentation issue on how to make your theme understand what regions are visibile, in that way Drupal dosnt take any desissions about your design and we dont lock anybody into the 3 columsn is what you have to work with. By keeping these inside of core there will be confusion on wtf is going on.
The
.admin
class sounds like a 90% issue :)Comment #10
paolomainardi CreditAttribution: paolomainardi commented@mortendk
Yes, i agree with you, i think that a class like ".page--$first-path-of-the-url" would cover enough.
Comment #11
wiifmHeya @mortendk!
Some thoughts on your patch:
great change, why was that even there in the first place.
watch the 80 char wrapping around here, also there is weird spacing issues around
arg()
Is
page--maintenance
enough, why do we needpage--maintenance-in-maintenance
as well?Happy class killing ;)
Comment #12
dead_armI've been outspoken about removing body classes, and I'm happy as long as the ones proposed make it in.
Comment #13
mortendk CreditAttribution: mortendk commented@dead_arm exactly we need to fine good sensible compromises :)
Comment #14
LewisNymanI think we could get rid of
.user-logged-in--not
because we could just use the :not() CSS selector here?If we do use it, it should more likely be:
.user-not-logged-in
because you wouldn't expect to inherit styles from.user-logged-in
. Actualy the two can't exist together!Comment #15
mortendk CreditAttribution: mortendk commentedComment #16
mortendk CreditAttribution: mortendk commentedok heres an update on the body classes:
* Removed the
.front / .not-front
classes as we have that with.path--frontpage
* Removed user-not-logged-in as lewis said these can be target with :not
Instead of the page-$something, renamed it to .path instead (cause it make sense), path-frontpage is added for the frontpage (and .front is removed) for consistency.
.node-type is renamed to .content-type--[foo] cause its a content type (let the bikeshed begin)
so to add a pink background for admin would be
.path-admin{background:pink}
Comment #17
GemVinny CreditAttribution: GemVinny commentedYeah I agree with Lewis about using :not.
Does there need to be both
.page--maintenance /.page--maintenance-in-maintenance
?Also mortendk you didn't change node-type to content-type. Want me to do it?
Comment #18
GemVinny CreditAttribution: GemVinny commentedOne more thing... it wasn't
.page-maintenance--active
that you added, it was.page--maintenance-in-maintenance
;-)Comment #19
mortendk CreditAttribution: mortendk commented@lilgemvinny + @wiifm
spot on no reason for 2 diffeent classes for the maintanence :)
Comment #20
mortendk CreditAttribution: mortendk commentedComment #21
LewisNymanI support this proposal.
It feels like we're using variants a bit inconsistently right now. We should only be using them if there is an original CSS component that is modified, not just for any adjective that describes something. So if you have '.page--maintenance' you would expect to find '.page' which would have some styling applied to it. But there is not :-)
Comment #22
mortendk CreditAttribution: mortendk commented@lewis
yup somehow that sneaked it self in - gonna run another clean on it later
Comment #23
mortendk CreditAttribution: mortendk commentedremoved the wrongly used modifier -- fron the classnames
Comment #24
mortendk CreditAttribution: mortendk commentedComment #25
dead_armTested patch in #23, and getting classes as desired, except page-maintenance when in maintenance mode. Attached screenshot. Not sure if this is because of a core bug.
Also need to clean up patch to follow core code standards for php before committing.
Comment #26
tim.plunkettI think this '' is causing an extra space in the classes array.
We're working to remove arg, so I'm not sure we want to do this. Also the PHP standards are off here, need a space after if, no space in the (), and the else should be on its own line
No change needed here.
This rename isn't okay. Even though we refer to Content Type in the UI, everywhere in code it is a node type.
Comment #27
mortendk CreditAttribution: mortendk commented@deadarm the maintainance-page is only set if you dont have acces to the pages. guess thats by design ? Dont know if it make sense to add it in, as the page for people that dont have acces to the site, have a page thats completely stripped from content (open up a chrome browser in anonymous mode & you get the empty page)
@timp well if we dont know whats gonna happen with arg(0) then let us fix that when / if its gonna happen - what were doing in this patch is cleaning up the stuff that we have allready & is creating unusable classes like .page-foo- .
alright rerolled it and removed the dumb stuff ;)
Comment #28
LewisNymanGuys! I think I'm reviewing PHP! What's going on?
One too many spaces here
I've never seen spaces before semicolons anywhere else in core?
We need a space between } and else.
Comment #29
mortendk CreditAttribution: mortendk commenteddear @LewisNyman yes you can totally do php - you are allowed to bye alchohol - then your grown up enough to do php ;)
anyways i cleaned up my codemess - so here ya gogo (wake me up)
Comment #30
LewisNymanSorry dude but you missed this space here
Comment #31
mortendk CreditAttribution: mortendk commented1 less spase
Comment #32
idflood CreditAttribution: idflood commentedSorry for yet another whitespace nitpick. According to https://drupal.org/coding-standards#controlstruct there is still some little coding standard issue (unless this changed for d8):
Comment #33
dawehnerCan't explain how embarassed I am about that ... do we want to actually use the internal path or the path alias as well?
We do have drupal_is_front_page for that
Comment #34
mortendk CreditAttribution: mortendk commented@dawehner : I think we wanna use the path alias, the internal dosnt matter - its what the themer can see in the adress bar , cause that make sense.
so your saying to not use arg(0) - then what should we use instead (me shwoing no knowledge of d8:)
Comment #35
mortendk CreditAttribution: mortendk commentedthis patch
removed the call on
arg(0)
concerning tims commens usingrequest_path()
insteadTesting on
drupal_is_front_page()
for frontpage class as wellComment #36
dawehnerNope, $request->getPathInfo() is certainly more the thing you wanna use.
Comment #37
mortendk CreditAttribution: mortendk commentedusing $path = \Drupal::request()->getPathInfo(); now for the gods of oop
opening up a bikeshed:
Wondering if
.path-foo
is the right classname?it make sense in the case of its the path but in a normal speaking term would it make more sense to have use "page" or "section" instead ?
I dont think that Core needs to think all these options in its a thing for a basetheme.
so can i get a thumps up / down for using
.path-$foo
as the basic classname, or something else that would make sense.hope to close the bikeshed soon ;)
Comment #38
joelpittetIMO, thumbs down, don't include the path/url parts at all in the classes.
Maybe you can give some good examples of the big overarching benefits this gives us over just using the node-type?
Here's how I see this going from handy to OMG, why did we do this?
Also if you still think this is a good idea, make sure you don't get language prefixes /fr /ca /us etc resulting in
for all your paths. So needs tests for the above.
Comment #39
aspilicious CreditAttribution: aspilicious commentedYeah I'm not found of the url alias either. The internal ID ==> ok, the alias ==> no go.
Comment #41
mortendk CreditAttribution: mortendk commentedSorry gentlemen i do think your both wrong(tm) The reason is that we creating a system that besides of being as clean as we can make it, also want to have sensible defaults.
nid: Using $nid is not a reliable usecase - using node id's for anything is a disaster waiting to happen (and the reason we removed em from node.html.twig) and do not fall down to a even 15% usecase. actually its one of the reasons for this patch.
path classes
Using the first bit of the path is as joel actually shows in his example is a 90% usecase, and honestly just make sense Just because a path can not be usable sometimes (as the example of multilangual sites, where the site is not using subdomains) is not the same as its not usable to have the path, or "section" as its called in zen theme, or pathone-, .page-, etc as its called other places.
Having a body class based on the name of the first path makes sense to 90% of the themers out there.
My bikeshed question was should it be called
.path-$foo
or.page-$foo
not do we even want it there, cause we do as it plan just make sense & is not cruft.Comment #42
aspilicious CreditAttribution: aspilicious commentedThe problem with aliases is the following, aliases can change, id's can't.
So if the client decides to rename the contact page and change it's url your page is potentially screwed.
By setting the default to alias you give themers "the impression" that they can safely use those classes.
I'm not going to fight this as I'm not going to be the guy theming the pages, but I'll be the guy that has the to explain to the client why the layout of the page gets screwed when they changed the title.
Anyway, have fun finishing this patch :)
Comment #43
joelpittetI'm not for ID's either BTW they are kinda safe as they won't change often but they may change between dev,stage, production environments, so glad they are gone and don't give false hopes to themers. So there are other solutions for the uniqueness that can be applied as needed. Like page based classes, or panels, or context module classes, etc...
The path alias seems to be trying to solve the "site section" based styling. Though I'd rather leave that up to the themer/base_themer to come up with the pattern proposed and not include this in core. One less class pattern to worry about and leave it up to the creativity of the themer to deal with how these extra classes should be handled.
Comment #44
mortendk CreditAttribution: mortendk commented@joelpittet
Yup exactly its the section issue + a bit of quick n easy solution that were trying to sold (well actually its more cleaning up in all the baseclasses and at the same time streamline the
.page-, .page-$nid, .page-$section
that im trying to solve. Simply cause its a very usable class to have, for all kinds of hackery.Maybe its there more as a convinience aka "we always had it" argument.
Anyways the reason for this path is not to remove the .page class its to cleanup the bodyclasses.
Comment #45
RainbowArrayIf we're talking about whether it's better to have path-foo or page-foo as a class, I'd say path-foo is better. That makes it clearer what the class name is all about. I don't like page-foo, as there is a lot of content on Drupal that isn't a "page". The more we can get away from everything being a page the better, in my opinion.
One other note. Am I understanding this correctly that foo represents only the first part of the path before any additional slashes? If that's the case, Maybe path-root-foo would be clearer. Although if that is the case that does make me question the usefulness of this class, since it would only be useful for changing styles based on top-level sections.
All I know is that I vote path-foo over page-foo on this issue.
Comment #46
dawehnerWe should use drupal_html_class here.
I guess we want to have at least some really basic test coverage.
Comment #47
LewisNymanComment #48
LewisNymanLet's figure this out in Austin
Comment #49
mortendk CreditAttribution: mortendk commentedreroll & claimed
Comment #50
aburrows CreditAttribution: aburrows commentedjust reviewing now :)
Comment #51
aburrows CreditAttribution: aburrows commentedThere was 1 class not defined correctly during testing:
maintenance-page
are we working withpage-maintenance
?Comment #52
mortendk CreditAttribution: mortendk commentedwe use the the word "maintenance-page" as it cause that what it is - its a maintenance page ;)
the whole in-maintenance etc is left overs from the good old days
Comment #53
aburrows CreditAttribution: aburrows commentedAwesome in which case this patch works as intended ;)
Comment #54
tim.plunkettThis needs to follow coding standards. spaces around the if, and else on new lines.
Also, this issue needs a real title.
Comment #55
mortendk CreditAttribution: mortendk commentedComment #56
mortendk CreditAttribution: mortendk commentedComment #57
mortendk CreditAttribution: mortendk commentedtestbot engage!
Comment #58
mortendk CreditAttribution: mortendk commentedgonna hunt this one down
Comment #59
mortendk CreditAttribution: mortendk commentedComment #60
mortendk CreditAttribution: mortendk commentedComment #61
LewisNymanI had a look over this to check for regressions in Stark and Bartik and I couldn't find anything.
Comment #65
sqndr CreditAttribution: sqndr commentedThe last patch does not apply any more, it needs a reroll.
Comment #66
sqndr CreditAttribution: sqndr commentedHere's that reroll. Hopefully testbot will be happy now!
Comment #67
mortendk CreditAttribution: mortendk commentedtestboot is happy! :)
Comment #68
star-szrComment #69
sqndr CreditAttribution: sqndr commentedHere's a new patch - it needed another reroll.
Comment #72
sqndr CreditAttribution: sqndr commentedComment #73
superspring CreditAttribution: superspring commentedFreeing this up for someone else...
Comment #74
amitgoyal CreditAttribution: amitgoyal commentedReroll of #69.
Comment #75
LewisNymanI manually tested that all the classes should appear as described in the issue summary again. Looks good!
Comment #76
alexpottWe need a change record for this.
Do we need the
.in-maintenance
here now? We might be able to dropfrom template_preprocess_maintenance_page too?
Comment #77
joelpittetA few more little things to take care of in this patch.
This doesn't do this path-$contenttype. Should this doc line be removed?
This should be single quotes.
This variable is not being used and the line can be removed as well.
Seems .in-maintenance is being used instead of .maintenance-page {}
Should this be replaced from bartik's:
or if that was meant to be like the issue summary and .maintenance-page is used, it should be replaced in seven's:
Comment #78
lauriiiI fixed points pointed by @joelpittet in #77. I also changed
.in-maintenance
to.maintenance-page
like the issue summary says.Comment #79
joelpittetThanks for the fixes @lauriii! Everything looks perfect except one typo:
This still needs to be an array.
Comment #80
lauriiiUps.. Changed it back to an array :)
Comment #82
G-raph CreditAttribution: G-raph commentedI'm going to review this.
Comment #83
rteijeiro CreditAttribution: rteijeiro commentedIt seems official tag is FUDK (kinda weird, BTW)
Comment #84
G-raph CreditAttribution: G-raph commentedI installed a minimal drupal8 and activated the Stark theme.
Initial bodyclasses: html, not-front, logged-in, page-user, page-user-, page-user-%id
Then I installed the patch from comment #80.
After patching the bodyclasses were: user-logged-in, path-user
Everything worked fine with no errors.
Screenshots (before and after) in attach.
Comment #85
G-raph CreditAttribution: G-raph commented.install-page is out of scope from this issue, but the styling on this class is removed.
All the rest is OK by me.
Comment #86
mortendk CreditAttribution: mortendk commentedJust to be the devils advocat we need screenshots also from :
Seven
Bartik
Comment #87
mortendk CreditAttribution: mortendk commentedJust to be the devils advocat we need screenshots also from :
Seven
Bartik
Comment #88
G-raph CreditAttribution: G-raph commentedI fixed my own remark from #85.
Comment #89
G-raph CreditAttribution: G-raph commentedI created screenshots (before and after) from Bartik and Seven themes, this was asked in #86. Everything is OK.
I do have 1 remark about the contenttype-bodyclass: in the summary there is a class defenition of ".content-type-$nodetype", but after testing the bodyclass is "node--type-$nodetype". (but I think this will be OK)
Comment #90
lauriiiMaintenance page seems to work also! We still need a change record.
Comment #91
GemVinny CreditAttribution: GemVinny commentedI'm going to add a change record.
Comment #92
jwilson3Per #89.
Comment #93
GemVinny CreditAttribution: GemVinny commentedI have created a change record for this.
Comment #94
GemVinny CreditAttribution: GemVinny commentedhttps://www.drupal.org/node/2330261
Comment #95
rteijeiro CreditAttribution: rteijeiro commentedFixed issue summary.
Comment #96
rteijeiro CreditAttribution: rteijeiro commentedReviewed change record and seems good.
Also there are screenshots provided in #89 and #90.
Is it RTBC now?
Comment #97
sqndr CreditAttribution: sqndr commentedThis looks good to me now as well.
Comment #98
star-szrI updated the change record to more accurately reflect what's happening in the latest patch, thanks for getting it going @lilGemVinny!
This jumped out at me, but this variable appears to be completely unused and undocumented. Looks safe to remove but might be safer to do so in a separate issue.
Comment #99
star-szrAlso I don't think we're doing anything with the layout classes, they look to be already gone (or I'm missing something but the patch certainly doesn't seem to be doing anything with them). Removing that from the title and making it more title-y.
Comment #100
star-szrUpdating the issue summary.
Comment #101
RainbowArrayThe layout classes are the ones you referenced in #98 I believe.
Comment #102
alexpottComment #103
LewisNymanMinor reroll in light of #2321505: Split Seven's style.css into SMACSS categories
Comment #104
RainbowArrayOnly thing that changed in last patch is the file names. Patch still applies. Back to RTBC.
Comment #105
RainbowArrayActually back to RTBC.
Comment #106
alexpottthe documentation in theme_get_suggestions() needs to be updated
In node-add.css - Will this still work? I don't think so.
In node.preview.js this code now might be redundant if the node-preview class no longer exists - I think we need a follow to remove it
is in bartik's style css - I don't think this will work any more.
Comment #107
rteijeiro CreditAttribution: rteijeiro commentedI'll try to cover @alexpott suggestions :)
Comment #108
lauriiiCreated issue for #98: #2341983: Remove unused variable from template_preprocess_page()
Comment #109
rteijeiro CreditAttribution: rteijeiro commentedI think I fixed some @alexpott suggestions. Let me know if I should do something else :)
Also created a followup for
node-preview
class stuff: https://www.drupal.org/node/2341995Comment #110
rteijeiro CreditAttribution: rteijeiro commentedComment #111
lauriii.page-admin
class is still here so I brought back styles attached to that class. I also fixed block demo mode by adding a class to the demo block so we can attach styles there instead of body class.Comment #112
rteijeiro CreditAttribution: rteijeiro commentedCould we get rid of ids?
This too.
This too.
This.
And this.
Comment #113
lauriiiI'm not sure if I was clear enough in my comment but
.page-admin
still exists so there shouldn't be need to remove these lines. Putting back to review!Comment #114
rteijeiro CreditAttribution: rteijeiro commentedOk, please read carefully my suggestion. We should try to follow CSS coding standards so we should clean-up that code and try to avoid using ids. Could we use classes instead? Pretty pleaaaaase?
Comment #115
lauriiiI actually first miss read your comment at first but the situation is that we cannot optimize the selectors anymore since there is now classes available for these elements. Putting back to RTBC. Lets get this in and party o/
Comment #116
mikl CreditAttribution: mikl commentedReviewed #111. Wonderful to get rid of some of those useless classes, great job.
Tests pass and everything, lets get it in :D
Comment #117
tim.plunkettSuggested retitle:
Remove useful body classes that shortsighted people assume no one will need
Comment #118
mortendk CreditAttribution: mortendk commented@tim - which classes is that you are referring to i guess its the "html" class ;)
Comment #119
tim.plunkettThis.
Comment #120
mikl CreditAttribution: mikl commentedThat's a somewhat misleading subset of the diff. The path-based suggestions are still there, just implemented differently.
Comment #121
mortendk CreditAttribution: mortendk commentedsidebar first & sidebar last classes is only relevant for a theme that is a 3 column layout + its regions is called "sidebar_first" and "sidebar_last"
bartik have both of those classes - it might be shortsighted but im pretty sure (ill bet you a beer & a pretzel) that the trend 2010 trend "3 column layouts" will change a bit the next couple of years when that responsive thing catches on ;)
Comment #122
rteijeiro CreditAttribution: rteijeiro commentedComment #123
joelpittetSorry all, but this seems to be a css regression if we are just removing admin specific theming.
If this is to happen there needs to be alternative admin theming to account for the change.
Can you please provide before/after screenshots?
@tim.plunkett I think that "sort-sighted" comment may need a bit of explanation why that is the case.
It could very well be but telling a blind person they are blind doesn't help them see.
EDIT: had wrong diff but same message:
Comment #124
lauriii.page-admin
class is still there so theming admin pages specificly should be possible. What we have removed is possibility to theme specific pages.Comment #125
LewisNymanI can fix this by adding the classes into the Seven theme.
Comment #126
LewisNymanOk, I've added a cleaner class to node add/edit pages in Seven. Much better than wild carding classes! It could do with a php review though...
This didn't get removed, it just shifted up the file to fit better.
The intention of the dreammarkup initiative has always been to serve default classes that most people will need, instead of trying to catch all the edge cases, otherwise we end up added a load of classes just in case they are needed by someone. It's ok to let people add their own classes and the banana initiative makes it a lot easier to control classes in the theme layer. Yay to less PHP!
Comment #127
lauriiiPatch #126 fixes the issue described on #123. Lets stop this bikeshedding.
Comment #128
alexpottAre these not all .path-admin now? To test this I guess bartik needs to be the admin theme
Comment #129
mortendk CreditAttribution: mortendk commentedgood catch renamed the classes to the correct path-admin
Comment #130
lauriiiPutting back to RTBC, fixes issue pointed by alexpott. To test this Bartik needs to be the admin theme.
Comment #132
mikl CreditAttribution: mikl commentedShaddap, stupid test bot (interdiffs should not be tested).
Comment #133
catchCould we also use Drupal::request->getPathInfo() here?
Also
Is this definitely no longer needed? Great if so.
Comment #134
lauriiiI dont see any functionality existing on Drupal core for #133.2. Fixed #133.1 in my patch
Comment #135
tuutti CreditAttribution: tuutti commentedCouldn't find any page.tpl using layout variable. Putting back to RTBC.
Comment #136
catchOK. Committed/pushed to 8.0.x, thanks!
Comment #138
mortendk CreditAttribution: mortendk commentedWoooooohooooo ! :)
Highfives all around
Comment #139
Wim LeersThe CR was never published. I just did.
Comment #141
zJoriz CreditAttribution: zJoriz commentedWow, you guys put a lot of work in this.
That said, does nobody want body classes for seeing if there's a View on deck, and if yes, which one? Or can I arrange that some other way (or did I simply miss the point here... distinct possibility)?
In most (if not all) of the sites I build, Views layouts differ quite a bit from Node layouts, usually starting at h1. Arguably I could set the classes for the Views as default and then make an exception for Nodes/Users/Taxonomy (since they can be detected).