Problem/Motivation
While, for example, nodes are rendered with classes to target theming by node type (bundle) and view mode, core doesn't provide anything parallel for content blocks.
Maybe this reflects the hybrid nature of blocks. The block content entity type that supports bundles and view modes is only one of many types of blocks. And since the addition of classes was moved from preprocessors to templates, modules cannot directly make their additions. Rendering classes in block.html.twig
would arguably privilege the core block_content
module over other (contrib) modules that also provide blocks but don't have the ability to inject their classes.
But whatever the source of this issue, the lack of such classes is a barrier when theming custom blocks and, compared to node
, detracts from parallelism between content entity types.
Proposed resolution
- Add
block_content_preprocess_block()
to conditionally set variables forbundle
andview_mode
. - In the
block.html.twig
template in Classy, add classes for bundle (type) and view mode parallel to those added for nodes innode.html.twig
.
Remaining tasks
User interface changes
API changes
Block templates receive two new variables for content blocks: bundle
and view_mode
.
Data model changes
Blocks in Classy are rendered with classes in the form block--type-image-[bundle]
and block--view-mode-[view_mode]
where [bundle] and [view_mode] are the bundle name and view mode name, both rendered with "clean" class names (e.g., hyphens in the place of underscores). Example with bundle of image_link
and view mode of full
: block--type-image-link
and block--view-mode-full
.
Comment | File | Size | Author |
---|---|---|---|
#36 | 2830725-36.patch | 1.94 KB | smustgrave |
| |||
#36 | interdiff-31-36.txt | 4.23 KB | smustgrave |
#17 | 2830725-17.patch | 6.94 KB | Kumar Kundan |
#14 | interdiff_2-14.txt | 1.36 KB | Kumar Kundan |
#14 | 2830725-14.patch | 2.85 KB | Kumar Kundan |
Comments
Comment #2
nedjoComment #9
Krzysztof DomańskiComment #11
sd9121 CreditAttribution: sd9121 as a volunteer and commentedComment #12
sd9121 CreditAttribution: sd9121 as a volunteer and commentedComment #13
Kumar Kundan CreditAttribution: Kumar Kundan as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedComment #14
Kumar Kundan CreditAttribution: Kumar Kundan as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedFor the test case, I followed the below step.
But after checking this I did not found, drupalGet() return class name of the newly created custom block.
Please correct me if I am wrong.
Comment #15
Kumar Kundan CreditAttribution: Kumar Kundan as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedI worked for my failed test case & I found when I changed in twig file & run test case(core/tests/Drupal/KernelTests/Core/Theme/ConfirmClassyCopiesTest.php), I found a hash value is generated against the changed file which is not matched.
The file location of HASH Value matching is core/tests/Drupal/KernelTests/Core/Theme/ConfirmClassyCopiesTest.php.
Can anyone help me to match the HASH value of the changed file?
Comment #16
Kumar Kundan CreditAttribution: Kumar Kundan as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedComment #17
Kumar Kundan CreditAttribution: Kumar Kundan as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedComment #18
Andrew Gorokhovets CreditAttribution: Andrew Gorokhovets as a volunteer commentedComment #19
Kumar Kundan CreditAttribution: Kumar Kundan as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedsorry for the noise, un-assigning so others may review it
Comment #22
Yasser Samman#17 does the job.
Tested it on multiple block types and in layout builder.
One thing to mention if you are using a custom theme, you need to add the classes to your block.html.twig.
The following is for easy reference:
I'll keep the status "Needs Review" so maybe someone else could try it out too.
Comment #23
ankithashettyJust rerolled the patch in #17, thanks!
Comment #24
smustgrave CreditAttribution: smustgrave at Mobomo commentedJust tested #23 and works for me. Moving to reviewed.
Comment #25
alexpottWe still need test coverage and a change record. As the changes in the issue show a themer will need to update a theme in order to take advantage.
Also I'm wondering if using type here is correct - at least without indicating that we mean content block type. This feels like it might be confusing because other blocks do not have a type. Same for view mode.
Comment #29
sthomen CreditAttribution: sthomen commentedHere's a reroll of the patch in #17 for 9.5
Comment #30
_utsavsharma CreditAttribution: _utsavsharma at OpenSense Labs for DrupalFit commentedCore/Theme/ConfirmClassyCopiesTest.php
themes/bartik/templates/block.html.twig
core/themes/classy/templates/block/block.html.twig
could not find these files.
Rerolled for 10.1.x.
Please review.
Comment #31
smustgrave CreditAttribution: smustgrave at Mobomo commentedAdded a simple asserting to an existing test.
Also please this belongs under the block_content module.
Would like some feedback before making the change record.
Comment #32
smustgrave CreditAttribution: smustgrave at Mobomo commentedComment #33
larowlanI'm not convinced that this is a good idea at all. We're conflating a block_content concept into a block template.
I have no worries about adding the prepreprocess hook, but adding them by default to the base templates feels like overreach.
Comment #34
sahilgidwani CreditAttribution: sahilgidwani at Axelerant commentedChecked and applied patch it is working for 10.1.x
Comment #35
smustgrave CreditAttribution: smustgrave at Mobomo commentedPlease read the comments and tags too. #33 discuss this not being the correct approach and it was tagged for change record.
Comment #36
smustgrave CreditAttribution: smustgrave at Mobomo commentedChanged preprocess to add classes instead vs relying on template.
Took a shot at the CR also.
Comment #37
larowlanI'm still not convinced adding these classes across the board is the right approach.
Personally I want less Drupal noise in my templates, not more.
Should we be instead taking the approach node takes, which is to add additional theme suggestions?
i.e so block--block-content--{bundle} and block--block-content--{bundle}--{view-mode} works out of the box?
Comment #38
larowlanThat seems to be what we're doing in #3062376: Template suggestions for custom block view modes
Comment #39
smustgrave CreditAttribution: smustgrave at Mobomo commentedCan see both scenarios where one may want to add css targeting a specific class without having to override the template.
Comment #40
AaronMcHaleI'm sort of leaning that way as well. It always bothers me how much markup Drupal can spew out onto the page. There's also the question of sites which may not even want to expose their internal naming to end users. It's probably not a security concern, but personally I might find it a bit annoying.
Building on that thinking, this does make me wonder if we should instead provide template suggestions for all [content] entities out of the box. This is a case of boilerplate code which at some point every entity type (core or contrib) is probably going to come up against.
Comment #41
larowlanYeah, I still think #3062376: Template suggestions for custom block view modes is the best approach here, @AaronMcHale seems to agree. Should we close won't fix this?
Comment #42
larowlanI asked in #frontend for others to chime in.
Comment #43
larowlanFor posterity, for a very long time our frontend workflow has been to use something like BEM to bring order to class naming, preferring to override templates to use class names that are meaningful to the project.
Theming using Drupal generated classes is considered an edge-case/last resort when you can't change the markup and typically isolated in CSS files under the heading 'Fugly selectors' 😛
Comment #44
smustgrave CreditAttribution: smustgrave at Mobomo commentedFYI not married to the idea here if the consensus is it’s not needed.
Adding a preprocess hook is also very simple.
Comment #45
nedjoWhen I opened this issue lo these several years ago, my concerns (see the issue summary) were two:
#2830725: Add block classes for bundle and view mode is a good step towards both concerns, so FWIW I agree this can be closed as a duplicate.
Comment #46
rikki_iki CreditAttribution: rikki_iki at PreviousNext commentedI'd prefer to avoid this... theme suggestions and variables sure, but classes output by default means more things I need to override and strip out.
Comment #47
larowlanThanks @smustgrave @nedjo @rikki_iki