Reproducing is very simple if you have Views:

(No, it's not a Views bug!)

  1. Create a new view of Content
  2. Add filter for Published
  3. Expose it
  4. Unrequire it
  5. Make its default - Any -
  6. 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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rudiedirkx’s picture

Status: Active » Needs review
FileSize
699 bytes
cilefen’s picture

This 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.

rudiedirkx’s picture

Status: Needs review » Closed (duplicate)
Related issues: +#1333910: Radio element does PHP type juggling when setting "checked" attribute

Yes, dear christ, and it's almost 4 years old. Same Views use case too. Good job guys!

Stuff like

Needs to go in Drupal 8 first.

kills it.

Thanks for the link @cilefen. Duplicate indeed.