TypeError when having exposed form in block and setting "role" attribute on it

Created on 2 February 2025, 2 months ago

Problem/Motivation

I admit this is a very specific problem, but when you have a Views exposed form in a block and set the ARIA "role" attribute on the form then any page displaying that block will return a TypeError like this:

TypeError: mb_strtolower(): Argument #1 ($string) must be of type string, array given in mb_strtolower() (line 225 of core/lib/Drupal/Component/Utility/Html.php).

Steps to reproduce

  1. Create a page view with exposed filters and configure it to have the exposed form in a block.
  2. Place the block.
  3. Alter the exposed form in some way to place the "role" attribute on it โ€“ i.e., like this: $form['#attributes']['role'] = 'search';
  4. Navigate to any page displaying the exposed form block.
  5. You will get an error page with a TypeError or a WSoD, depending on your configuration.

Proposed resolution

The problem is either in template_preprocess_block() or in \Drupal\views\Plugin\Block\ViewsExposedFilterBlock::build(). The former assumes that $variables['label'] is always a string/scalar (when it passes it unchecked to Html::getUniqueId() at the bottom of the function), while the latter sets $output['#title'] to a render array which will apparently make it unchanged to $variables['label'] when rendering the block.

I assume the bug is in template_preprocess_block(), as the render array in $output['#title'] otherwise seems to work fine, thatโ€™s why I picked this component.

Remaining tasks

  • Decide which code is correct and which is buggy.
  • Create an MR for fixing the problem.
  • Create a regression test.

User interface changes

None.

Introduced terminology

None.

API changes

None.

Data model changes

None.

Release notes snippet

N/A

๐Ÿ› Bug report
Status

Active

Version

11.1 ๐Ÿ”ฅ

Component

block.module

Created by

๐Ÿ‡ฆ๐Ÿ‡นAustria drunken monkey Vienna, Austria

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @drunken monkey
  • ๐Ÿ‡ฆ๐Ÿ‡นAustria drunken monkey Vienna, Austria
  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand quietone
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia niranjan_panem Gurugram

    I tested the issue, views-pages and exposed forms are working fine in drupal, but not able to reproduce the issue.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia niranjan_panem Gurugram
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States bnjmnm Ann Arbor, MI

    Given that I've had to reopen several issues incorrectly closed by @niranjan_panem and

    I tested the issue, views-pages and exposed forms are working fine in drupal, but not able to reproduce the issue.

    Doesn't provide enough detail to indicate the steps to reproduce were followed, this may not have received the due diligence required to justify setting this to Closed (cannot reproduce)

  • First commit to issue fork.
  • Pipeline finished with Failed
    about 2 months ago
    Total: 129s
    #420020
  • Pipeline finished with Failed
    about 2 months ago
    Total: 144s
    #420029
  • I have implemented a fix to ensure that when $variables['label'] is an array, it gets rendered into a string before assigning it to title_attributes['id']. This prevents the TypeError when setting form attribute $form['#attributes']['role'] = 'search';

    Additionally, I have added a PHPUnit test to verify this.

  • Pipeline finished with Failed
    about 2 months ago
    Total: 500s
    #420039
  • Pipeline finished with Success
    about 2 months ago
    Total: 5700s
    #420076
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Issue summary needs some work as the proposed solution is not flushed out.

    Left a comment on the MR.

  • Pipeline finished with Failed
    about 2 months ago
    Total: 95s
    #420594
  • Pipeline finished with Failed
    about 2 months ago
    Total: 846s
    #420599
  • Pipeline finished with Success
    about 2 months ago
    #420626
  • Moved the Views exposed filter test to the Search integration test file, as this test verifies that when the search role is assigned to the Views exposed form, the aria-describedby attribute is correctly set without a TypeError. This issue occurs only when the Views exposed form label is rendered as an array.

    As per the MR comment, I have also added test coverage for the Block module by adding the role attribute for a standard block and verifying the normal label scenario.

  • ๐Ÿ‡ฆ๐Ÿ‡นAustria drunken monkey Vienna, Austria

    @riyas_nr: This looks great to me, thanks a lot for your work on this! Setting to RTBC.

    I amended the โ€œProposed resolutionโ€ section of the IS.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    Couple of comments on the MR.

Production build 0.71.5 2024