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 for bundle and view_mode.
  • In the block.html.twig template in Classy, add classes for bundle (type) and view mode parallel to those added for nodes in node.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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nedjo created an issue. See original summary.

nedjo’s picture

Version: 8.2.x-dev » 8.3.x-dev
Issue summary: View changes
Status: Active » Needs review
FileSize
1.93 KB

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Krzysztof Domański’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests, +Needs change record

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

sd9121’s picture

Assigned: Unassigned » sd9121
sd9121’s picture

Assigned: sd9121 » Unassigned
Kumar Kundan’s picture

Assigned: Unassigned » Kumar Kundan
Kumar Kundan’s picture

For the test case, I followed the below step.

  1. Create a basic page.
  2. Create a block & assigned to basic page.
  3. With the assertRegExp() try to verify the class name.

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.

Kumar Kundan’s picture

Assigned: Kumar Kundan » Unassigned

I 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?

Kumar Kundan’s picture

Assigned: Unassigned » Kumar Kundan
Kumar Kundan’s picture

Andrew Gorokhovets’s picture

Status: Needs work » Needs review
Kumar Kundan’s picture

Assigned: Kumar Kundan » Unassigned

sorry for the noise, un-assigning so others may review it

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Yasser Samman’s picture

#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:

{%
  set classes = [
    'block',
    'block-' ~ configuration.provider|clean_class,
    'block-' ~ plugin_id|clean_class,
     bundle ? 'block--type-' ~ bundle|clean_class,
     view_mode ? 'block--view-mode-' ~ view_mode|clean_class,
  ]
%}

I'll keep the status "Needs Review" so maybe someone else could try it out too.

ankithashetty’s picture

Just rerolled the patch in #17, thanks!

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Just tested #23 and works for me. Moving to reviewed.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

We 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.

+++ b/core/profiles/demo_umami/themes/umami/templates/classy/block/block.html.twig
@@ -30,6 +33,8 @@
+    bundle ? 'block--type-' ~ bundle|clean_class,
+    view_mode ? 'block--view-mode-' ~ view_mode|clean_class,

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.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

sthomen’s picture

Here's a reroll of the patch in #17 for 9.5

_utsavsharma’s picture

Core/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.

smustgrave’s picture

Added 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.

smustgrave’s picture

Status: Needs work » Needs review
larowlan’s picture

Issue tags: +Blocks-Layouts

I'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.

sahilgidwani’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
47 KB

Checked and applied patch it is working for 10.1.x

smustgrave’s picture

Status: Reviewed & tested by the community » Needs work

Please read the comments and tags too. #33 discuss this not being the correct approach and it was tagged for change record.

smustgrave’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record
FileSize
4.23 KB
1.94 KB

Changed preprocess to add classes instead vs relying on template.

Took a shot at the CR also.

larowlan’s picture

+++ b/core/modules/block_content/block_content.module
@@ -51,6 +51,19 @@ function block_content_theme($existing, $type, $theme, $path) {
+    $variables['attributes']['class'][] = 'block-type--' . $variables['elements']['content']['#block_content']->bundle();
+    if (isset($variables['elements']['#configuration']['view_mode'])) {
+      $variables['attributes']['class'][] = 'view-mode--' . $variables['elements']['#configuration']['view_mode'];

I'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?

larowlan’s picture

smustgrave’s picture

Can see both scenarios where one may want to add css targeting a specific class without having to override the template.

AaronMcHale’s picture

I'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?

I'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.

larowlan’s picture

Yeah, 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?

larowlan’s picture

I asked in #frontend for others to chime in.

larowlan’s picture

For 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' 😛

smustgrave’s picture

FYI not married to the idea here if the consensus is it’s not needed.

Adding a preprocess hook is also very simple.

nedjo’s picture

When I opened this issue lo these several years ago, my concerns (see the issue summary) were two:

the lack of such classes is [1] a barrier when theming custom blocks and, [2] compared to node, detracts from parallelism between content entity types.

#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.

rikki_iki’s picture

I'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.

larowlan’s picture

Status: Needs review » Closed (duplicate)

Thanks @smustgrave @nedjo @rikki_iki