Single on/off checkbox is always checked

Created on 18 November 2015, over 9 years ago
Updated 20 May 2025, 14 days ago

Problem/Motivation

Import the attached test view. (It has an exposed boolean filter for Content: Published initially set to No, and in the BEF form it's set to display as "Single on/off checkbox".) Observe the checkbox for Published in preview is on when it should be off. Unchecking and clicking Apply doesn't help. Same happens if you add a page display and try it there.

This happens because of the following three things:

  1. an unchecked checkbox does not appear in GET (or POST) which leads to problems. This is a well known, researched, patched to death problem in core (the checkbox element broke dozens of times before a test was written for Drupal 7 which covers some 60 possible cases putting it to rest finally. See the double loop in FormCheckboxTestCase and the related issue linked.).
  2. Views loads the defaults into $form_state['input'] in many filter's value_form:
          if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
            $form_state['input'][$identifier] = $this->value;
          }
    
  3. better_exposed_filter reuses #default_value set by Views for a #select . That's not correct for a checkbox.

Proposed resolution

Add a form element to signal when the form is submitted and calculate the value of the checkbox ourselves based on $_GET. Make sure the code is secure.

Remaining tasks

Perhaps write a test. I do not feel the need really. The current code mirrors form_type_checkbox_value close enough.

πŸ› Bug report
Status

Fixed

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States ksenzee Washington state

Live updates comments and jobs are added and updated live.
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.

Production build 0.71.5 2024