Attributes of a block content are applied to block itself

Created on 10 May 2015, over 9 years ago
Updated 8 February 2023, almost 2 years ago

If a block returns a renderable array and the array has direct attibutes they will be "stolen" from the original content and applied to the block itself.

For example a if a block returns something like this:

array(
  '#attributes' => array(
    'class' => array(
      'foo-bar'
    ),
    'data-test' => 'This is just a test'
  ),
  '#markup' => 'This is block content'
);

The rendered HTML will be:

<div id="block-foobartest" class="foo-bar" data-test="This is just a test">
  This is block content.
</div>

instead of:

<div id="block-foobartest">
  <div class="content foo-bar" data-test="This is just a test">
    This is block content.
  </div>
</div>

This is especially bad for blocks that represent forms since none of the form attributes is preserved.

πŸ› Bug report
Status

Needs work

Version

10.1 ✨

Component
BlockΒ  β†’

Last updated 4 days ago

Created by

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States scottsawyer Atlanta

    I am running into this problem with exposed forms in blocks, but it only surfaced when I updated to D 9.5. I was using views_block_filter_block because my views were block displays and I needed the exposed filters in separate blocks. It seems like the D9 update superseded views_block_filter_block, changing where the attributes were attached.

    I patched with the MR, but it doesn't seem to be working, exposed form class attributes are still being moved to the containing block. If you have a custom block template for blocks containing views exposed forms that don't output all of the attributes, it breaks the ajax as well as the CSS in my case.

    I worked around the issue by refactoring my template structure, I was fortunate to find a relatively simple solution, unfortunate that it cost me half a day to discover the problem and fix it.

Production build 0.71.5 2024