Problem/Motivation
All classes are produced in a preprocess that makes it hard for a themer to manipulate these into whatever they need.
Proposed resolution
Add all classes at the top of the Twig templates, when applicable (more than 3 classes or classes built with logic).
{%
set classes = [
'block',
'block-' ~ provider|clean_class,
]
%}
<div{{ attributes.addClass(classes) }}>
Preprocess Functions Modified
core/modules/block/block.module: template_preprocess_block CONVERTED
core/modules/search/search.module: search_preprocess_block LEFT AS CONTRIB WOULD ADD
core/modules/system/system.module: system_preprocess_block LEFT AS CONTRIB WOULD ADD
core/themes/bartik/bartik.theme: bartik_preprocess_block LEFT AS CONTRIB WOULD ADD
Twig Templates Modified
core/modules/system/templates/block.html.twig CONVERTED
core/themes/bartik/templates/block--system-branding-block.html.twig EXTENDS from block.html.twig
Test instructions
Create a new node.
Make a note of the markup that a field produces.
Apply the patch.
Now check the markup of the field again it should match the markup from before the patch was applied.
Comment | File | Size | Author |
---|---|---|---|
#24 | move_block_classes_out-2328913-24.patch | 4.26 KB | lauriii |
Comments
Comment #1
seantwalshComment #2
lanchez CreditAttribution: lanchez commentedI'll look into this now.
Comment #3
lanchez CreditAttribution: lanchez commentedOk so, here's a patch that removes classes from template_preprocess_block and adds them to block twig template. I also fixed tests that failed.
There's still stuff to do with blocks that wants to add more classes. Pretty much system_preprocess_block and bartik_preprocess_block are only ones. I couldn't really figure out how system_menu_block works so kinda need help with that.
Comment #4
seantwalshThanks @lanchez! Also, need to remove content_attributes, title_attributes, etc. that add classes in preprocess. For instance, in the case of $variables['content_attributes']['class'][] = 'content';. Since this is the only class added and it is simple, you can add it directly inline like:
<div{{ content_attributes.addClass('content') }}>
Comment #5
lanchez CreditAttribution: lanchez commentedYes, I was thinking about that but there's a separate issue for it http://drupal.org/node/1972122
Comment #6
seantwalshAh, hadn't seen that. So this issue will depend on that going in.
Comment #7
star-szrI don't think this should be blocked on #1972122: Remove the DIV tag around block content, this is much more straightforward and either one can be rerolled :)
I think we should focus on template_preprocess_block() and block.html.twig in this issue.
system_preprocess_block() and bartik_preprocess_block() are such special cases, if we want to add more templates for those that's fine but not sure it has to happen in this issue.
search_preprocess_block() should be left as is IMO, it's an optional module. If we want to pull that out it and create a template it can be its own issue.
Thanks for working on this @lanchez!
Comment #8
lanchez CreditAttribution: lanchez commentedJust moved content class to twig. This might fail tests since class order did change on some blocks.
So should we do more in this issue or not?
Comment #10
star-szrI think we can fix up the tests and wrap this one up. Thanks @lanchez!
Comment #11
lanchez CreditAttribution: lanchez commentedNow the test is "fixed" if you know what I mean. The classes should be checked from rendered block, but I'm not very good with tests. So either it's fine like that or someone should the test better.
Comment #12
lanchez CreditAttribution: lanchez commentedComment #13
joelpittet@lanchez Nice work! That fail seems to be on the content class test. Do you think you can try debugging that?
The issue summary suggests we should look at all those preprocesses and templates suggestions. There is a chance we don't need to do anything for those but we should address them never the less in-case we miss something.
Comment #14
joelpittetWow sorry, I cross posted with you and all sorts of things went weird. Here's a re-upload for testbot.
Comment #15
star-szrLooks good! I manually tested and things (attribute order and class order) moved around a bit but nothing of consequence.
Comment #16
joelpittetJust an FYI regarding the test. It's correct the way you did that. I didn't think so at first but it's testing what comes of the preprocess and that is not in the preprocess, so to test that you'd have to render the block and do xpath or something. Though the plan is to remove that class anyways so... that'd be total busy work... RTBC +1
Comment #17
joelpittetOh wait, @Cottser thoughts on #13 second paragraph regarding the issue summary?
Comment #18
joelpittetActually yeah it needs work,
Needs to be moved into a search template, and the block classes need to be added there as well.
As well as the other items mentioned in the issue summary.
Comment #19
star-szr@joelpittet I addressed that in #7. I think if we want to go that far and add all those templates (don't forget we need to add them to
hook_theme()
as well if they are new module templates) then IMO that should happen in a separate issue.Personally I think things like search_preprocess_block() are not defining default classes but rather are acting the way contrib will, just adding classes to the default list. I think we should be focussing more on template_preprocess_* functions, that is where we will have the biggest impact. Because the reality is there are classes coming from other places than preprocess so this initiative as it is will not be removing all classes from core markup.
Comment #20
joelpittet@Cottser I get ya on the preprocess, what about system branding block, we've lost classes on those blocks that were previously there. Which I'd expect a visual regression,no?
Comment #21
joelpittetWill check for myself after breakfast:p
Comment #22
joelpittetOk Updated the issue summary, and back to RTBC. The other blocks are using @extend :)
Comment #24
lauriiiRerolled
Comment #25
RainbowArrayLooks good. Patch still applies. Back to RTBC.
Comment #26
alexpottLet's open a followup to get rid of BlockPreprocessUnitTest it looks completely pointless.
Committed 4d2c54d and pushed to 8.0.x. Thanks!
Comment #28
joelpittetFollowup issue created #2339069: Remove useless BlockPreprocessUnitTest