Reproducing is very simple if you have Views:
(No, it's not a Views bug!)
- Create a new view of Content
- Add filter for Published
- Expose it
- Unrequire it
- Make its default - Any -
- Save it, and the edit it
The selected default value is now No, which is wrong, because the actual value is "All"
. If you view the HTML source, you'll see both - Any-
and No
are checked
.
This is Drupal's fault, because of:
if (isset($element['#return_value']) && $element['#value'] !== FALSE && $element['#value'] == $element['#return_value']) {
$element['#attributes']['checked'] = 'checked';
}
in theme_radio()
in form.inc
. (It's really PHP's fault for comparing strings and numbers like a crazy person, but we all know that.)
The default value is 0
, a potential value is "All"
, and that of course is equal in PHP!
The fix is pretty simple too, assuming radio values (#options
's keys) and #default_value
are always scalar:
Instead of
$element['#value'] == $element['#return_value']
check
(string) $element['#value'] === (string) $element['#return_value']
because "0"
and "All"
aren't identical.
Comment | File | Size | Author |
---|---|---|---|
#1 | drupal-radios-default-value-2543376-1.patch | 699 bytes | rudiedirkx |
Comments
Comment #1
rudiedirkx CreditAttribution: rudiedirkx commentedComment #2
cilefen CreditAttribution: cilefen commentedThis sounds like a duplicate. I feel as though I have seen this issue worked on, perhaps #1333910: Radio element does PHP type juggling when setting "checked" attribute.
Comment #3
rudiedirkx CreditAttribution: rudiedirkx commentedYes, dear christ, and it's almost 4 years old. Same Views use case too. Good job guys!
Stuff like
kills it.
Thanks for the link @cilefen. Duplicate indeed.