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

Comments

Chi created an issue. See original summary.

andrewmacpherson’s picture

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

chi’s picture

Issue tags: -Accessibility

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

amanire’s picture

Issue tags: +Accessibility

Sorry, 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.

amanire’s picture

Issue tags: -Accessibility

Of course now that I say that, I discovered this old WebAIM thread and specifically

I guess it does not affect a screen reader since they only can pull up a list of inputs, not labels without an input.

so I will defer to @andrewmacpherson here. Would be nice to have more data to support this either way though.

andrewmacpherson’s picture

Re. #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: pointer inappropriately 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.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

chi’s picture

Another minor issue here is cursor: pointer CSS rule that core themes apply to the label element. In case of item element it makes no sense as clicking on its label has no effect.

chi’s picture

Status: Active » Needs review
StatusFileSize
new6.05 KB

The patch introduces a new element property #title_tag.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new160 bytes

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

ranjith_kumar_k_u’s picture

StatusFileSize
new4.85 KB
new1.44 KB

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

anantaisaustin’s picture

Version: 11.x-dev » 10.3.x-dev
StatusFileSize
new4.23 KB

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

quietone’s picture

Version: 10.3.x-dev » 11.x-dev

Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

intrafusion’s picture

Status: Needs work » Needs review
StatusFileSize
new4.46 KB

Patch re-rolled for D11

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

Thanks! But fixes need to be in MRs. Also looking at the summary it appears to be incomplete.

mgifford’s picture

I'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.

mgifford’s picture

Status: Needs work » Closed (duplicate)

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.