Fix undefined array key warning in sortNestedOptions when option key is empty

Created on 22 April 2025, 7 days ago

Problem/Motivation

When using Better Exposed Filters with sorted filter options, a PHP warning is triggered if one of the options has an empty string ("") as its key. This is a valid use case — for example, to represent a blank/default filter option — and it works functionally (the filter renders correctly and operates as expected), but it throws a warning:

Warning: Undefined array key "" in Drupal\better_exposed_filters\BetterExposedFiltersHelper::sortNestedOptions()

This happens specifically inside the sortNestedOptions() method when trying to access the value of a previous key without validating its existence first.

This warning clutters logs, can affect debugging workflows, and may trigger issues in stricter CI environments or when logging is set to be verbose.

Steps to reproduce

  • 1. Create a View with an exposed filter using a sorted widget (e.g., radio buttons or checkboxes).
  • 2. Enable “Sort options alphabetically” or relabel filter options via the Better Exposed Filters settings.
  • 3. Ensure one of the filter options uses an empty string key — for example, to represent “All” or “None”.
  • 4. View the page or block with the exposed filter.
  • 5. Observe the PHP warning in logs or output.

Proposed resolution

Patch the sortNestedOptions() method in BetterExposedFiltersHelper.php to:

  • Check whether the previous key ($prev_key) exists in $flat_options before accessing it.
  • Fallback to an empty string or skip the concatenation if the key is not defined.

This resolves the warning without changing the module’s behavior or affecting existing filter rendering logic.

🐛 Bug report
Status

Active

Version

7.0

Component

Code

Created by

🇺🇸United States Buelto Boston

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

Comments & Activities

Production build 0.71.5 2024