Collapsible checkboxes filter doesn't stay open if only first option is checked

Created on 3 February 2025, 2 months ago

Problem/Motivation

I have a Checkboxes/Radios filter that is collapsible. It's a multivalued field, so it's checkboxes. When I check only the first option, the filter collapses as if no options have been selected. If I check more than one option or any option after the first option, the advertised default behavior works as expected.

Steps to reproduce

See above.

Proposed resolution

I don't think this has anything to do with the recent work related to Issue โœจ Allow not automatically opening collapsed filter Needs review

The problematic code has been around for ages: https://git.drupalcode.org/project/better_exposed_filters/-/blame/7.0.x/...

Add here #2985623: Secondary options closed after submit โ€“ reversion โ†’

      // Check if one or more values are set for our current element.
      $options = $form[$element]['#options'] ?? [];
      $default_value = $form[$element]['#default_value'] ?? key($options);
      $has_values = array_reduce($user_input, function ($carry, $value) use ($form, $element, $default_value) {
        return $carry || ($value === $default_value ? '' : ($value || $default_value === 0));
      }, FALSE);

The problem is that for my checkboxes, there is no default value for the filter, which means that $default_value gets set to
key($options);, which returns the first option. That means the array_reduce ends up finding that the user-selected value is equal to the default value and sets $has_values to false.

I would propose that we not consider the default value in this logic. I would personally want to see the filer open if any value has been selected, including a default value.

Remaining tasks

User interface changes

API changes

Data model changes

๐Ÿ› Bug report
Status

Active

Version

6.0

Component

Code

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States danflanagan8 St. Louis, US

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

Comments & Activities

Production build 0.71.5 2024