Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
If the return value for a checkbox is 0 (zero), then that checkbox will always be selected as default. I believe the root of this issue is to be found in theme_checkbox() on the following line:
if ($element['#value'] !== 0 && $element['#value'] == $element['#return_value']) {
The problem is that if #return_value is 0 and #value is empty, then the loose comparison (==) will result in the two values to be deemed equal. The result is that the particular checkbox will be selected. I believe a possible solution to this problem would be to make sure that #value never is empty, but always defaults to 0 if it is empty. In other words, one could simply do the following:
function theme_checkbox($variables) {
$element = $variables['element'];
//Validate #value and ensure that it defaults to 0 if empty.
$element['#value'] = !empty($element['#value']) ? $element['#value'] : 0;
$t = get_t();
Comments
Comment #2
matslats CreditAttribution: matslats commentedplus one
Comment #3
bakr CreditAttribution: bakr commentedplus two