Checkboxes can never be used for Group filters with <5 options

Created on 22 March 2023, over 1 year ago
Updated 5 June 2024, 4 months ago

Problem/Motivation

When using a Grouped filter with less than 5 options it is imposible to use checkboxes via the BEF RadioButtons plugin.

Core views make the below decision in FilterPluginBase when working out what form element to use when rendering a Grouped filter:

$form[$value] = [
        '#title' => $this->options['group_info']['label'],
        '#type' => $this->options['group_info']['widget'],
        '#default_value' => $this->group_info,
        '#options' => $groups,
      ];
      if (!empty($this->options['group_info']['multiple'])) {
        if (count($groups) < 5) {
          $form[$value]['#type'] = 'checkboxes';
        }
        else {
          $form[$value]['#type'] = 'select';
          $form[$value]['#size'] = 5;
          $form[$value]['#multiple'] = TRUE;
        }
     }

If you have a Grouped filter with less than 5 options that allows mutiple value selection, you will recieve a checkboxes element with no "#multiple" property set.

The decision to use checkboxes in the BEF RadioButtons plugin is then taken based on the existance of the non-existant '#multiple' proprty:

// Render as checkboxes if filter allows multiple selections.
      if (!empty($form[$field_id]['#multiple'])) {
        $form[$field_id]['#theme'] = 'bef_checkboxes';
        $form[$field_id]['#type'] = 'checkboxes';

        // Show all/none option.
        $form[$field_id]['#bef_select_all_none'] = $this->configuration['select_all_none'];
        $form[$field_id]['#bef_select_all_none_nested'] = $this->configuration['select_all_none_nested'];

        // Attach the JS (@see /js/bef_select_all_none.js)
        $form['#attached']['library'][] = 'better_exposed_filters/select_all_none';
      }

Steps to reproduce

  • Cerate a multiple select Group filter on a View. Give it less than 5 options
  • Enable the Radio/Checkbox bef widget for the field
  • View the filter. Will be renderd as radios, when it should be rendered as checkboxes.

Proposed resolution

Update the if condition in RadioButtons to use checkboxes if the provided element is already using checkboxes.

πŸ› Bug report
Status

Fixed

Version

6.0

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom lind101

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024