Consider this form element.
$form['example'] = [
'#type' => 'item',
'#title' => $this->t('Example title'),
'#markup' => $this->t('Example value'),
];
When rendered it produces the following markup:
<div id="edit-example" class="js-form-item form-item js-form-type-item form-type-item js-form-item-example form-item-example">
<label for="edit-example">Example title</label>
Example value
</div>
As stated in the Mozilla documentation:
for attribute contains the id of a "labelable" form-related element in the same document as the <label> element.
In this case <div> is used which is certainly not a "labelable" form-related element.
The relevant question on SO.
https://stackoverflow.com/questions/12195052/is-it-allowed-to-use-label-...
| Comment | File | Size | Author |
|---|---|---|---|
| #22 | 3082011-22.patch | 4.46 KB | intrafusion |
Comments
Comment #2
andrewmacpherson commentedThanks for filing this @Chi
As this stands, I don't think it causes any practical accessibility harm. It's just a bad HTML pattern, which will be nice to fix.
Comment #3
chi commentedComment #5
amanire commentedSorry, but how is this not an accessibility issue? Screen readers expect a form control to be associated with a
<label>element. That is their primary purpose, see e.g. https://www.w3.org/WAI/tutorials/forms/labels/. They are even styled in the seven theme with the pointer cursor specifically to signify that they can be clicked to enter data in an input.This actually appears to be a regression in Drupal core on the node form Last Saved and Author labels, see https://www.drupal.org/project/drupal/issues/2249089. And the generalized approach described here seems like a good idea.
Comment #6
amanire commentedOf course now that I say that, I discovered this old WebAIM thread and specifically
so I will defer to @andrewmacpherson here. Would be nice to have more data to support this either way though.
Comment #7
andrewmacpherson commentedRe. #6:
Yes, that's exactly what I meant in #2. Orphaned labels (without an input) are effectively just spans.
Overall I agree that the use of the
<label>element for'#type' => 'item'is something to address in terms of good HTML use. It's just not a practical accessibility problem, because there is no form input here.Re. #5:
That's a very good point about the use of
cursor: pointer. If it gives the impression that there is an operable control here, when there isn't, that's a problem.We're using
cursor: pointerinappropriately in lots of places. Many web designers and developers treat it as if it means "clickable", which is at odds with the CSS recommendation which says: "The cursor is a pointer that indicates a link". The human interface guidelines for desktop applications on WIndows, Mac, and Gnome also say that the hand-pointer icon is specifically for links, not other clickable controls. The CSS rec. and the OS guidelines are in harmony on this. However we're using it for select, label, and buttons too. That's worth filing as a separate issue.Comment #13
chi commentedAnother minor issue here is
cursor: pointerCSS rule that core themes apply to the label element. In case ofitemelement it makes no sense as clicking on its label has no effect.Comment #14
chi commentedThe patch introduces a new element property
#title_tag.Comment #16
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #17
ranjith_kumar_k_u commentedComment #19
anantaisaustinThis worked for us. I rerolled this patch by removing the demo_umami reference and it is working to fix the accessibility error on our site that was caused by an orphan form label.
Comment #20
quietone commentedChanges are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies.
Comment #22
intrafusionPatch re-rolled for D11
Comment #23
smustgrave commentedThanks! But fixes need to be in MRs. Also looking at the summary it appears to be incomplete.
Comment #24
mgiffordI'm going to try to bring @intrafusion's patch over to https://www.drupal.org/project/drupal/issues/2279111
I will close this as a duplicate.
Comment #25
mgifford