Problem/Motivation
Since block_class 3.1.0, classes configured on a block are added in
`hook_block_view_alter()` instead of `hook_preprocess_block()` following
#3590322.
This can inadvertently prevent attributes provided by the block content from
being merged into the block render array.
A visible regression occurs with Views exposed filter blocks:
1. The exposed form root render element contains classes such as
`views-exposed-form`.
2. Better Exposed Filters additionally adds `bef-exposed-form`.
3. `BlockViewBuilder::preRender()` moves `#attributes` from the block content
to the top-level block render array using a shallow array union.
4. Because `block_class` has already added a `class` key to the top-level
`#attributes`, the `class` values coming from the content are discarded.
5. As a result, neither the block wrapper nor the `
` element carries
`views-exposed-form` / `bef-exposed-form` anymore.
Example markup before block_class 3.1.0:
<div class="views-exposed-form bef-exposed-form front-filter pt-3 block block-views block-views-exposed-filter-blockoffers-offers-page">
Example markup after block_class 3.1.0:
<div class="front-filter pt-3 block block-views block-views-exposed-filter-blockoffers-offers-page">
The form itself is rendered without those classes as well:
<form action="/example" method="get" id="views-exposed-form-example-page" accept-charset="UTF-8">
This breaks CSS and JavaScript relying on the standard Views/BEF classes.
Steps to reproduce
1. Enable `block_class` 3.1.0 and `better_exposed_filters`.
2. Create a View with an exposed form displayed as a block.
3. Place the exposed filter block.
4. Add any custom class to the block using Block Class.
5. Inspect the generated markup.
Expected result: the configured block class and the exposed form classes are
all present, as with block_class 3.0.x.
Actual result: only the configured block class remains;
`views-exposed-form` and `bef-exposed-form` are missing.
Proposed resolution
Restore the previous behavior by adding configured classes in
`hook_preprocess_block()`, after core has moved the block content attributes,
or otherwise delay/merge the Block Class additions after
`BlockViewBuilder::preRender()` has processed the block content attributes.
A `#pre_render` callback appended after the core block pre-render callback may
be an alternative if avoiding the extra block load is still desired.
Related issues
- #3590322: Improve block class rendering
- #3263435: Views exposed form as block results in form attributes being added
to the block div instead of the form element
Issue fork block_class-3641904
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
ericdsd commentedComment #3
ericdsd commentedComment #4
ericdsd commentedComment #5
ericdsd commentedComment #8
dydave commentedI think the breaking change was work carried assisted with AI.
Had an AI agent run for about an hour, with few prompts, 3 or 4 failed build pipelines.
Most of the time was spent running tests and various validation jobs phpcs, phpstan, etc...
Generated reply is below:
Thanks @ericdsd for the detailed report and the analysis, it was spot on.
I'd rather not roll back #3590322. The root cause is that core's
BlockViewBuilder::preRender()merges the attributes of the block content into the block with a shallow union (+=). Sinceclassis already set when Block Class adds its classes inhook_block_view_alter(), theclassvalues coming from the content are dropped, and then removed from the content.This is not specific to Views: it affects any block whose content has
classin#attributesor#wrapper_attributes. Before core 11.4, core moves all the content#attributes; since 11.4 it moves#wrapper_attributes(and#attributesfor content without#typeor#theme).The merge request keeps
hook_block_view_alter(), which avoids the extraBlock::load()of the old preprocess hook, and adds the classes afterwards:#block_class_classesand a#pre_rendercallback is appended.BlockClassPreRender(TrustedCallbackInterface) merges them into#attributes['class']once core has merged the content attributes.BlockClassContentAttributesTest, with a small test block, for#wrapper_attributesand#attributes. It fails without the fix. The#wrapper_attributescase is skipped on core older than 11.4, which does not support it..gitlab-ci.yml: fixed the Drupal 10 detection of thephpstan (previous major)job and simplified the attribute removal with afindloop.Merge request: !91
Pipeline (all required jobs pass): 1070283
Testing: the tests pass locally on Drupal 11.5 and in the pipeline on the previous major (10.x), previous minor (11.3), next minor and next major jobs. I have not tested with Better Exposed Filters installed, so a manual check on a site with Views exposed filter blocks and BEF would be appreciated:
views-exposed-form,bef-exposed-formand the custom class are all present.