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

Command icon 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

ericdsd created an issue. See original summary.

ericdsd’s picture

Issue summary: View changes
ericdsd’s picture

Issue summary: View changes

dydave made their first commit to this issue’s fork.

dydave’s picture

Status: Active » Needs review

I 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 (+=). Since class is already set when Block Class adds its classes in hook_block_view_alter(), the class values 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 class in #attributes or #wrapper_attributes. Before core 11.4, core moves all the content #attributes; since 11.4 it moves #wrapper_attributes (and #attributes for content without #type or #theme).

The merge request keeps hook_block_view_alter(), which avoids the extra Block::load() of the old preprocess hook, and adds the classes afterwards:

  • The cleaned classes are stored in #block_class_classes and a #pre_render callback is appended.
  • The new BlockClassPreRender (TrustedCallbackInterface) merges them into #attributes['class'] once core has merged the content attributes.
  • New kernel test BlockClassContentAttributesTest, with a small test block, for #wrapper_attributes and #attributes. It fails without the fix. The #wrapper_attributes case is skipped on core older than 11.4, which does not support it.
  • .gitlab-ci.yml: fixed the Drupal 10 detection of the phpstan (previous major) job and simplified the attribute removal with a find loop.

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:

  1. Place the exposed filter block.
  2. Add a class with Block Class.
  3. Verify that views-exposed-form, bef-exposed-form and the custom class are all present.