When I add title to a radio button, this way:
$form['select_consultant']['choices'][$number] = array(
'#id' => 'radio'.$radio_id,
'#type' => 'radio',
'#title' => $title,
'#return_value' => $number,
'#default_value' => 0,
'#parents' => array('choices'),
'#spawned' => TRUE,
);
the output gets rendered as
<label class="option"><input type="radio" name="choices" value="1" class="form-radio" />something</label>
but Label functionality (when I click the label's text, the field gets selected) doesn't work this way in IE6. When I put in the for attribute, it works again perfectly in IE6:
<label class="option" for="radio1"><input type="radio" name="choices" value="1" id="radio1" class="form-radio" />something</label>
I know that according to W3C HTML 4.01 recommendation it doesn't need to have for attribute, but can we add it in? 60% of normal people still use IE6 and they would welcome the usability improvement.
W3C:
for = idref [CS]
This attribute explicitly associates the label being defined with another control. When present, the value of this attribute must be the same as the value of the id attribute of some other control in the same document. When absent, the label being defined is associated with the element's contents.
I corrected the theme_radio function in includes/form.inc to add for attribute, but I have no idea how to make a patch - if someone made it and posted it here, i'd be grateful.
function theme_radio($element) {
_form_set_class($element, array('form-radio'));
$output = '<input type="radio" ';
$output .= 'name="'. $element['#name'] .'" ';
$output .= 'value="'. $element['#return_value'] .'" ';
if (!empty($element['#id'])) { //add this if
$output .= 'id="'. $element['#id'] .'" ';
}
$output .= (check_plain($element['#value']) == $element['#return_value']) ? ' checked="checked" ' : ' ';
$output .= drupal_attributes($element['#attributes']) .' />';
if (!is_null($element['#title'])) {
if (!empty($element['#id'])) { //and this one - only add for if we know element's id
$output = '<label class="option" for="'. $element['#id'] .'">'. $output .' '. $element['#title'] .'</label>';
} else {
$output = '<label class="option">'. $output .' '. $element['#title'] .'</label>';
}
}
unset($element['#title']);
return theme('form_element', $element, $output);
}
What do you think about adding this to Drupal?
| Comment | File | Size | Author |
|---|---|---|---|
| #15 | issue-261284-15.patch | 3.28 KB | lilou |
| #14 | 261284.patch | 3.23 KB | roychri |
| #11 | 261284.patch | 3.38 KB | roychri |
| #7 | form.patch | 1.58 KB | keff |
| #4 | radio_for.patch | 1.45 KB | stevenpatz |
Comments
Comment #1
damien tournoud commentedThis looks reasonable. Could you provide a proper patch?
Reassigning to the development queue (7.x) because this bug is also there. Once corrected, a back-port will have to be made for Drupal 6.
Comment #2
keff commentedThe same goes for checkbox (it seems strange to me that radio buttons can be without id, but theme_checkbox uses $element[#id] without checking for emptiness - does anyone know why? Also phpdoc comment for it doesn't say that id is used...):
corrected:
Comment #3
keff commentedThanks. Right now I cannot make patch - I have no idea how to do it, and during exam time, I don't have the time to install and understand all the infrastructure needed to create patch. If someone made a patch, I'd be very grafeful.
Comment #4
stevenpatzhere's a patch
Comment #5
keff commentedThanks! And, could you do another one for checkbox? Here is the code with corrected indentation:
Comment #6
damien tournoud commentedPlease do not try to diff between Drupal 6 and Drupal 7: the style convention for the concatenation operator changed. The patch needs work.
Comment #7
keff commentedOK, so I looked into it, made a patch against actual D7 (with D7 conventions) - drupal 'creating patches howto' was a big help - and tested the improved functionality in IE6.
Comment #8
damien tournoud commentedThe patch looks good, except for a small indentation problem.
This needs testing on several browsers to ensure that nothing breaks.
Comment #9
keff commentedWhich ones? I tried to stick to D7 style.
As for testing, it worked for me in IE6, IE7 and Opera 9, i tried IE 5.5 with IETester ( http://www.my-debugbar.com/wiki/IETester/HomePage ), but drupal's javascript was soo broken there that I had problems just navigating the admin (but that may be caused by experimental nature of IETester as well).
Comment #10
dries commentedThere are still some coding style issues with this patch. Also, it would be good if we could add some code comments. Not everyone knows what the for-attribute is used for.
Lastly, it would be great if you could fix your own issues/questions from comment #2. :)
Comment #11
roychri commentedphpdoc updated.
#id emptyness for checkbox fixed.
coding style fixed (checked using code-style.pl).
code comment about for-attributed added.
tested against latest version of drupal7. Performed an installation and then visited the admin/build/themes page where there's a lot of checkboxes and radio buttons.
no errors.
Comment #12
keff commentedThank you! I use this on one of my websites and I haven't encountered any problem with it, and since it is rather cosmetic update, I think it is ready to be put to D7 trunk.
Do you agree?
Comment #13
drewish commenteddoesn't apply any more.. there's also some spelling and grammar problems with the comments:
I don't think we need to mention IE6 since we're following the specification. How about this instead?
And for the checkbox comment you duplicated the radio button comment:
Comment #14
roychri commentedI am able to apply the patch in #11 to D7 CVS and to the tarball: http://ftp.drupal.org/files/projects/drupal-7.x-dev.tar.gz
I am doing it like this:
What am I doing wrong?
I made the changes to the comments, I like what you propose for the text.
Here is the new patch.
Comment #15
lilou commentedPatch #14 doesn't applied ... reroll against CVS/HEAD.
Comment #17
lilou commentedSee: #335122: Test clean HEAD after every commit and http://pastebin.ca/1258476
Comment #18
catchLooks good, seems like we could have a test which creates the form with radios/checkboxes, then does an assertRaw for the label-for though.
Comment #20
mgiffordLooked at the patch and seems like this is resolved, in core and backported to 6:
http://drupal.org/node/152098
The patch here was a bit more descriptive, but not sure it provides much more functionality. An empty 'for=""' is as good as a none as far as I am aware.
Wished I'd seen this before though.
Mike