Problem/Motivation

According to the HTML specification when using the "label" element the "for" attribute should only be used when the element you're labelling is "labelable". See: http://www.w3.org/html/wg/drafts/html/master/forms.html#the-label-element

Labelable elements are:
button
input (if the type attribute is not in the Hidden state)
keygen
meter
output
progress
select
textarea
form-associated custom elements

Steps to reproduce

  1. Install Drupal with the Standard profile.
  2. Visit /admin/config/media/file-system.
  3. Inspect the markup for "Public file base URL",

In addition to inspecting the markup, note the pointer as you hover over the text in Step 3.

Proposed resolution

  1. Add the property non_labelable to the Item element (Drupal\Core\Render\Element\Item).
  2. Add the variable tag to all versions of form-element-label.html.twig.
  3. In core/lib/Drupal/Core/Form/FormPreprocess.php, set that variable to label if the form element is labelable (and keep the existing markup). If not, then set tag to span, add an id attribute to it, and add an aria-describedby attribute to the wrapping <div>. (Since (1) applies only to Item elements, that is the only non-labelable form element.)

In future issues, we may add the non_labelable property to other form elements. In (3), the code just checks for the property, not the element type.

Remaining tasks

User interface changes

Here is the markup for the "Public file base URL" section: see the Steps to Reproduce. Both code snippets below use the Default Admin theme.

Before

<div data-drupal-wrapper-selector="edit-file-public-path" id="edit-file-public-path" class="js-form-item form-item js-form-type-item form-type--item js-form-item-file-public-path form-item--file-public-path">
            <label for="edit-file-public-path" class="form-item__label">Public file system path</label>
              sites/default/files
              <div id="edit-file-public-path--description" class="form-item__description">
      A local file system path where public files will be stored. This directory must exist and be writable by Drupal. This directory must be relative to the Drupal installation directory and be accessible over the web. This must be changed in settings.php
    </div>
  </div>

The <label> element has a for attribute referring to the wrapping <div>.

After

<div data-drupal-wrapper-selector="edit-file-public-path" id="edit-file-public-path" aria-describedby="edit-file-public-path--label" class="js-form-item form-item js-form-type-item form-type--item js-form-item-file-public-path form-item--file-public-path">
            <span id="edit-file-public-path--label" class="form-item__label">Public file system path</span>
              sites/default/files
              <div id="edit-file-public-path--description" class="form-item__description">
      A local file system path where public files will be stored. This directory must exist and be writable by Drupal. This directory must be relative to the Drupal installation directory and be accessible over the web. This must be changed in settings.php
    </div>
  </div>

The <label> element is replaced by a <span> element with an id attribute. The wrapping <div> refers to that element using aria-describedby.

Introduced terminology

None

API changes

None

Data model changes

None

Release notes snippet

N/A

Original report by specky_rum

According to the HTML specification when using the "label" element the "for" attribute should only be used when the element you're labelling is "labelable". See: http://www.w3.org/html/wg/drafts/html/master/forms.html#the-label-element

Labelable elements are:
button
input (if the type attribute is not in the Hidden state)
keygen
meter
output
progress
select
textarea

Currently the theme_form_element_label($variables) function takes no account of the type of the element it is labelling. As long as there's an ID for the element it adds this as the "for" attribute. This is done where it does this:

  if (!empty($element['#id'])) {
    $attributes['for'] = $element['#id'];
  }

The "for" attribute is not a required attribute so presumably we could just add an array of element names or Drupal form element types in this function to exclude the "for" attribute?

The reason this has come up is that a site we manage failed an accessibility check run by the Shaw Trust in the UK. The issue they raised was that there are labels without a corresponding element. In fact, they do have an element, it's just that it's a div. The specific example is a form created using the entityform module which includes a list of checkboxes. Now although that's a contrib module it uses the standard Drupal core field rendering code. An example of the resultant code for checkboxes is something like:

<div class="form-item form-type-checkboxes form-item-field-a3-posters-location-und">
  <label for="edit-field-a3-posters-location-und">Location </label>
 <div id="edit-field-a3-posters-location-und" class="form-checkboxes"><div class="form-item form-type-checkbox form-item-field-a3-posters-location-und-Adam-and-Eve-Street-car-park">
 <input id="edit-field-a3-posters-location-und-adam-and-eve-street-car-park" name="field_a3_posters_location[und][Adam and Eve Street car park]" value="Adam and Eve Street car park" class="form-checkbox" type="checkbox">  <label class="option" for="edit-field-a3-posters-location-und-adam-and-eve-street-car-park">Adam and Eve Street car park (2 sides; behind Grafton centre) </label>

</div>

The only problem with that code is that the label's "for" attribute refers to the id of a div which is not a "labelable" element.

We have got round this for the very specific instance of checkboxes by implementing theme_form_element_label($variables) in our theme but this is a very specific override and not appropriate to core. Our full override function is as follows (it will be extended to pick up other element types in future):

function theme_form_element_label($variables) {
  // run the regular theme function on it
  $output = theme_form_element_label($variables);
  
  // check if it's checkboxes
  if ($variables['element']['#type'] == 'checkboxes') {
    // if it is, strip out the "for" attribute
    $output = preg_replace('#\s(for)="[^"]+"#', '', $output);
  }
  return $output;
}

Issue fork drupal-2279111

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

dcam’s picture

Status: Active » Closed (duplicate)
Issue tags: +Accessibility
Related issues: +#2192419: Use a WCAG-compliant fieldset (fieldgroup) for #type radios/checkboxes

Work has been done in 8.x to correct this problem. Groups of radios and checkboxes now have a legend instead of a label. Work is on-going to continue improving these elements. See the related issue. Unfortunately, this can't be fixed in core for 7.x because the change would break every theme. There may be a contributed module that will override the theming to make it accessible. I'm closing this issue as a duplicate.

David_Rothstein’s picture

Version: 7.x-dev » 8.0.x-dev
Status: Closed (duplicate) » Postponed (maintainer needs more info)
Issue tags: +Needs backport to D7

I think this might still be relevant actually. Even if Drupal 8 fixed it for checkboxes and radios, is it still a problem for other elements?

We can't switch label to legend in Drupal 7 certainly, but simply leaving out the "for" attribute in these cases seems like it would be doable as a backport. However, would that really help accessibility - i.e. is pointing screen readers nowhere better than pointing them to a div? Seems like it might actually be worse...

Personally I've also come across this issue during automated accessibility scans of particular sites, but note #884846-39: Orphaned Labels Being Generated for Radios, Checkboxes & Items which suggests that this issue has "no real impact on users, only on testing tools". I used that as a justification not to bother fixing it on the site in question :)

mgifford’s picture

Issue tags: +label

Perhaps. I'm not sure what other elements though. Certainly every <label> should be associated with an <input> type with a for attribute. It was an issue for multipart forms like radios & checkboxes.

Hadn't heard of the http://www.shaw-trust.org.uk folks before...

Drupal 7 uses <label> incorrectly in form elements as you would a header. #2249089: Improper use of a LABEL for Last Saved & Author & #2250015: Improper use of a LABEL for user profile are some examples where we've fixed this in D8.

@David_Rothstein you asked "is pointing screen readers nowhere better than pointing them to a div? Seems like it might actually be worse..."

Removing the for would be not pointing them anywhere vs pointing them to a div. The point would be to remove the pointer.

I'm really not sure what elements are legitimately using labels, are available in Drupal Core, and aren't already covered. We are still waiting on a solution for #2192419: Use a WCAG-compliant fieldset (fieldgroup) for #type radios/checkboxes but I don't think that expanding theme_form_element_label($variables) function will help.

Non-labelable elements should not use a label, but rather should use a div.

dawehner’s picture

Component: base system » forms system

This fits better into the form subsystem

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

katannshaw’s picture

@specky_rum: I see that this is an old post but I thought I'd offer my thoughts on the subject. I think there are two ways to deal with this issue. I see the Forms API dealing with this properly on the Admin > Configuration > Text formats > Basic HTML page under "Enabled filters" when you inspect the code. Here's a code snippet:

<div id="filters-status-wrapper"><div id="edit-filters-status" class="js-form-item form-item js-form-type-item form-type-item js-form-item- form-item-">
      <label for="edit-filters-status">Enabled filters</label>
        <div class="js-form-item form-item js-form-type-checkbox form-type-checkbox js-form-item-filters-filter-html-status form-item-filters-filter-html-status">
        <input data-drupal-selector="edit-filters-filter-html-status" type="checkbox" id="edit-filters-filter-html-status" name="filters[filter_html][status]" value="1" checked="checked" class="form-checkbox">

        <label for="edit-filters-filter-html-status" class="option">Limit allowed HTML tags and correct faulty HTML</label>
      </div>
...

As you can see, the id of the wrapping <div> element matches the <label> element's "for" attribute, and then the inner <input> and <label> elements have matching id/for values.

The other way to deal with it is to place it into a <fieldset> element which would be a more proper way to do things. The Contact Form module does this, and it would be great of the Forms API would as well in the future if it doesn't already. See more info at https://developer.mozilla.org/en-US/docs/Web/HTML/Element/fieldset

-Kat

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

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
amber himes matz’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)
Issue tags: +Bug Smash Initiative

It looks like from #11, that Form API is handling this situation correctly, and best practice methods were suggested as well.

I think this can be closed as works as designed.

klonos’s picture

Status: Closed (works as designed) » Active

@katannshaw and @Amber Himes Matz admittedly this is a hard problem to fix, but at the same time I don't think that it can be marked as "works as designed".

To sum up the problem:

So this for example, is NOT proper use of the label and for attribute:

<div id="edit-my-item" >
  <label for="edit-my-item">Test item </label>
  <div class="description">Test item description.</div>
</div>

After some quick research, the best suggestion I have found is to use a combination of a <span> and the aria-describedby attribute. Something like this for instance:

<div aria-describedby="label-for-div" >
  <span id="label-for-div">Test item </span>
  <div class="description">Test item description.</div>
</div>

Noting the following from https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attribut... :

  1. the aria-labelledby attribute is not appropriate, because it should only be used for interactive elements (which div s are NOT)
  2. the aria-describedby is not ideal either, since it should be used for a description (longer text) rather than a label (short text)

The purpose of aria-labelledby is the same as that of aria-label . It provides the user with a recognizable, accessible name for an interactive element.

The aria-labelledby and aria-describedby attributes both reference other elements to calculate text alternatives. aria-labelledby should reference brief text that provides the element with an accessible name. aria-describedby is used to reference longer content that provides a description.

Reopening this issue in order to figure out a proper solution to this, rather than haste to dismiss it.

lauriii’s picture

Version: 9.3.x-dev » 11.x-dev
Status: Active » Needs review
StatusFileSize
new1.96 KB

This isn't implementing exactly what is in #17 which I think would be ideal. However, based on #3, it might be an improvement to remove the for attribute when the label is not used for phrasing content. Here's a patch that tries to add support for that in Form API, and adds a use of this to \Drupal\Core\Render\Element\Item.

smustgrave’s picture

Status: Needs review » Needs work

Should a follow up be made for the other points from #17. Think since this adds improvement it should still move forward. Could the issue summary be updated and tests added?

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.

benjifisher’s picture

I am removing the "Needs backport to D7" issue tag, since it is obsolete. I am not sure about the "label" tag. There are 35 other issues that have it, so maybe it is useful.

I agree with Comment #19, at least the part about needing test coverage. I am adding the tag for that.

I am pretty sure that #3082011: Do not use label HTML tag in "item" form element is a duplicate of this issue, and I prefer the approach here in Comment #18. If at least one person agrees with my opinion, then let's close the other issue.

I prefer the approach in Comment #18 to that in Comment #17. I think that a <label> element has more semantic meaning than a <span>, and we do not have to do extra work to style it. But I will defer to a11y experts and front-end developers.

I am adding #3490435: [regression] Form #states are processed and can be added to the wrong attribute for 'item' elements as a related issue. Maybe it is a stretch to do that, but both issues involve special handling for the item render element. Maybe the fix for one can inform the approach for the other.

Edit: from Comment #3,

Non-labelable elements should not use a label, but rather should use a div.

I defer to mgifford: unless standards have changed since that comment, or mgifford has some other reason to reconsider, I guess I am wrong about using <label>. See also the comments from andrewmacpherson on #3082011: Do not use label HTML tag in "item" form element (Comments #2, #7).

benjifisher’s picture

I want to check that by fixing this issue we will not be regressing some previous issue.

I got some help from Claude code. It finds that the id attribute on the wrapper div was added in #783438: #states doesn't work for #type item. The for attribute in the label is newer: #1938926: Convert simpletest theme tables to table #type.

The second issue was mostly about <table> markup. I did not review the comments, but I searched for "item" and did not find any reference to the item render element. It seems likely that it was not considered on that issue.

Based on that, I am reasonably confident that we can fix this issue without causing regressions.

benjifisher’s picture

I opened a MR. I used Claude Code, but I gave it a lot of direction and made several manual edits.

I tested manually. On the page /admin/config/media/file-system, I now see the markup

<div id="file-public-base-url-wrapper" aria-describedby="file-public-base-url-label" class="js-form-item form-item js-form-type-item form-type--item js-form-item-file-public-base-url form-item--file-public-base-url">
      <span id="file-public-base-url-label" class="form-item__label">Public file base URL</span>
        https://drupal.ddev.site/sites/default/files
            <div id="edit-file-public-base-url--description" class="form-item__description">
      The base URL that will be used for public file URLs. This can be changed in settings.php
    </div>
  </div>

I think that implements the recommendation from Comment #17.

The last commit updates the id attribute on the wrapper: it removes the "edit-" prefix and adds "-wrapper". The same commit adds the aria-describedby attribute and a matching id attribute on the span.

The last commit also updates the tests, but the changing id attributes mean that the test-only CI job will not be meaningful. The first two commits add test coverage, and checking out those changes is a better way to test without any code changes.

The commit dfa506dfe31 (Test #nonlabelable attribute, not #markup attribute) is arguably out of scope for this issue. It updates an existing check, intended for item elements, to use the new attribute. It may fix the related issue #3490435: [regression] Form #states are processed and can be added to the wrong attribute for 'item' elements.

I am leaving the status at NW because of static analysis errors. I will get to that later. I may add "labelable" to the Cspell dictionary.

benjifisher’s picture

Status: Needs work » Needs review

I added "labelable" to the dictionary and changed "nonlabelable" to "non_labelable".

Should I have used drupal-dictionary.txt instead of dictionary.txt?

mgifford’s picture

So after looking at https://www.drupal.org/project/drupal/issues/3082011 I thought there were elements of @intrafusion's which should be pulled into this.

This consolidation adds on top https://git.drupalcode.org/project/drupal/-/merge_requests/16464/diffs

1. Explicit `#non_labelable` metadata for wrappers like `#type => item`.
2. Programmatic association for non-labelable wrappers via:
- wrapper: `id="-wrapper"` + `aria-describedby="-label"`
- title: `-label">...` (or custom `#title_tag`)
3. Regression coverage proving the relationship (no invalid `label[for]` targeting `div`, no orphan title text).

Credit first: this approach builds directly on intrafusion's re-roll in #3082011 comment #16669292 (`3082011-22.patch`). Thank you, @intrafusion, for pushing the `#title_tag` + template simplification pattern forward.

#### 1. W3C / HTML spec compliance
Per HTML spec, `label[for]` is for labelable controls only (`input`, `select`, `textarea`, `button`, etc.). Pointing `for` at a `div` fails validation and creates noisy false positives in scanners.

#### 2. Accessible association via ARIA
Removing invalid `for` references is necessary, but not sufficient. We still need a programmatic relationship between wrapper and title for assistive tech.

This patch path does that by:
- marking elements as non-labelable (`#non_labelable => TRUE`),
- rendering wrapper ids as `-wrapper`,
- rendering title ids as `-label`,
- wiring wrapper `aria-describedby` to the title id.

So we get valid HTML and preserved accessibility semantics.

This is an AI generated consolidation, but made sense to me.

benjifisher’s picture

Status: Needs review » Needs work

@mgifford:

I left several suggestions on MR !16464. I am setting the status back to NW.

I think the main changes relative to MR !16441 are

  1. Do not change the id attribute on the wrapper div from edit-... to ...-wrapper. I already suggested, in a comment on MR !16441, that such a change might be a bad idea.
  2. Pass a tag variable to the Twig template in addition to non_labelable. (One of my suggestions is to pass it instead of, not in addition to.)
  3. Use the prefix --label instead of -label.

Testing MR !16464, I see the following markup on /admin/config/media/file-system:

<div id="edit-file-public-base-url" aria-describedby="edit-file-public-base-url--label" class="js-form-item form-item js-form-type-item form-type--item js-form-item-file-public-base-url form-item--file-public-base-url">
      <span id="edit-file-public-base-url--label" class="form-item__label">Public file base URL</span>
        https://drupal.ddev.site/sites/default/files
            <div id="edit-file-public-base-url--description" class="form-item__description">
      The base URL that will be used for public file URLs. This can be changed in settings.php
    </div>
  </div>

The only differences compared to the snippet in Comment #24 are the result of (1) and (3).

benjifisher’s picture

We need to update the issue summary with the standard template and provide at least the proposed resolution. I do not have time to do that now, but I will add the issue tag.

Both MRs have test coverage, so I am removing the tag for that.

This issue will also need a change record, so another issue tag.

Although I do not have time to rewrite it, I did review the current issue summary. It lists the labelable elements, and the list in the current summary agrees with https://html.spec.whatwg.org/multipage/forms.html#category-label, except that the list here includes keygen. In particular, Drupal's checkboxes element renders as a group of <input type="checkbox"> elements, wrapped in a <div>. According to the issue summary, the label (if present) refers to the wrapping div. But that seems to be obsolete. Looking at the configuration form for a block (Go to /admin/structure/block and follow the Configure link for any block) the "Response status" section has the markup (using the Claro theme)

<fieldset data-drupal-selector="edit-visibility-response-status-status-codes" aria-describedby="edit-visibility-response-status-status-codes--wrapper--description" id="edit-visibility-response-status-status-codes--wrapper" class="fieldgroup form-composite fieldset fieldset--group js-form-item form-item js-form-wrapper form-wrapper">
      <legend class="fieldset__legend fieldset__legend--composite fieldset__legend--visible">
    <span class="fieldset__label fieldset__label--group">Response status</span>
  </legend>
  
  <div class="fieldset__wrapper fieldset__wrapper--group">
        
        <div id="edit-visibility-response-status-status-codes" class="form-checkboxes form-boolean-group"><div class="js-form-item form-item js-form-type-checkbox form-type--checkbox form-type--boolean js-form-item-visibility-response-status-status-codes-200 form-item--visibility-response-status-status-codes-200">
          <input data-drupal-selector="edit-visibility-response-status-status-codes-200" type="checkbox" id="edit-visibility-response-status-status-codes-200" name="visibility[response_status][status_codes][200]" value="200" class="form-checkbox form-boolean form-boolean--type-checkbox">
        <label for="edit-visibility-response-status-status-codes-200" class="form-item__label option">Success (200)</label>
      </div>
<div class="js-form-item form-item js-form-type-checkbox form-type--checkbox form-type--boolean js-form-item-visibility-response-status-status-codes-403 form-item--visibility-response-status-status-codes-403">
          <input data-drupal-selector="edit-visibility-response-status-status-codes-403" type="checkbox" id="edit-visibility-response-status-status-codes-403" name="visibility[response_status][status_codes][403]" value="403" class="form-checkbox form-boolean form-boolean--type-checkbox">
        <label for="edit-visibility-response-status-status-codes-403" class="form-item__label option">Access denied (403)</label>
      </div>
<div class="js-form-item form-item js-form-type-checkbox form-type--checkbox form-type--boolean js-form-item-visibility-response-status-status-codes-404 form-item--visibility-response-status-status-codes-404">
          <input data-drupal-selector="edit-visibility-response-status-status-codes-404" type="checkbox" id="edit-visibility-response-status-status-codes-404" name="visibility[response_status][status_codes][404]" value="404" class="form-checkbox form-boolean form-boolean--type-checkbox">
        <label for="edit-visibility-response-status-status-codes-404" class="form-item__label option">Page not found (404)</label>
      </div>
</div>

                  <div id="edit-visibility-response-status-status-codes--wrapper--description" data-drupal-field-elements="description" class="fieldset__description">Shows the block on pages with any matching response status. If nothing is checked, the block is shown on all pages. Other response statuses are not used.</div>
    
      </div>
</fieldset>

So we should review the markup for other form elements. Perhaps item is not the only one that needs to be updated.

I thought perhaps that we could handle item the same way as checkboxes, but I think the <label> element is added in the preprocess function, so in fact we seem to be on the right track.

mgifford’s picture

StatusFileSize
new9.49 KB

This has been sitting here for a month, so this may not be a terrible time to see if this AI generated patch helps nudge things along.

The updated patch:

  • Introduces #labelable, defaulting to TRUE.
  • Sets #labelable => FALSE for #type => 'item'.
  • Renders item titles as instead of if empty.
  • Preserves existing wrapper IDs.
  • Removes aria-describedby, generated title IDs, #non_labelable, and #title_tag.
  • Tests visible and visually hidden item titles.
  • Updates all five current core template implementations.

Reviewing the interdiff this seems to almost flip many of the assumptions, I think it makes it easier to understand the code.

benjifisher’s picture

@mgifford:

This has been sitting here for a month, ...

Yes: a month ago I commented on your MR and on this issue, and you did not reply until yesterday.

... see if this AI generated patch helps nudge things along.

If you want to move this issue along, then it would be more effective to update your MR and/or comment on this issue.

I would like to work with you to get this issue to a good place, but I am not sure how to do that. Usually, one person takes the developer role, working on a merge request, and a second person takes the reviewer role. I am happy to take either role.

mgifford’s picture

@benjifisher 1st of all, sorry. I'm definitely still trying to wrap my head around how to work with this GitLab instance. Most of my commits have been historically done in Drupal with good old .patch files and maybe a few interdiffs.

I find the two interfaces quite confusing. I tossed up the .zip as I didn't want to mess up the MR.

Let me go take a look at your comments. I would rather not be the reviewer. I can work to see that the code "works" but I do limited day to day Drupal and haven't for a long while.

benjifisher’s picture

Issue summary: View changes
Issue tags: -Needs issue summary update

I am updating the issue summary and removing the related issue tag. I think this issue still needs a change record.

I reviewed the code and did another round of manual testing. (I used the test results in the issue summary.) It all looks good to me, but I am leaving the status at NW because this issue still needs a change record.

benjifisher changed the visibility of the branch 2279111-non-labelable to hidden.

benjifisher changed the visibility of the branch 2279111-consolidate- to hidden.

benjifisher’s picture

MR !16464 had modify/delete conflicts because the Claro theme was removed. I rebased and resolved the conflicts.

benjifisher’s picture

Issue summary: View changes
benjifisher’s picture

Issue summary: View changes
Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs change record

I added a draft change record, and I am updating the snippets in the issue summary to use the Default Admin theme instead of Claro.

The issue status was NW for a change record (Comment #34). Although I updated the MR and resolved conflicts (Comment #37) I am still taking the reviewer role, so I can set the status to RTBC.

benjifisher’s picture

Title: Labels should not have "for" attribute for non labelable elements » Form API 'item' elements should not use the 'for' attribute

I am changing the title to reflect the current scope of this issue: it affects only Form API elements with '#type' => 'item'. (But see the last line of the Proposed resolution.)

quietone’s picture

I read the comments, the MR (not a review) and the change record. The short version is that the core gates are passing. I have updated credit.

I prefer not to have words added to the dictionary but labelable is used on https://html.spec.whatwg.org/multipage/forms.html#the-label-element. Change records should start with the change instead of what happened 'previously'. But the change record is well written so no need to adjust it.

Leaving at RTBC

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Added some comments to the MR that need addressing.

mgifford’s picture

Status: Needs work » Needs review
StatusFileSize
new12.01 KB

Thank @alexpott I think I've addressed your points. I've added an interdiff. And yes, I've used AI.

benjifisher’s picture

Status: Needs review » Needs work

Back to NW: see my comments on the MR.

mgifford’s picture

Status: Needs work » Needs review

Spellcheck didn't like "labelability". Some CSS changes and simplification of the PHP. All organized with AI with my supervision.

AI summarizes the changes like so:

  • Removed the additional aria-describedby association and generated title IDs. Existing description references are preserved.
  • Removed #non_labelable in favour of the simpler #labelable property, retaining #title_tag to control title markup.
  • Render non-interactive item titles as <span> rather than <label>.
  • Preserve explicit label associations where a labelable target is provided.
  • Preserve existing wrapper IDs and inline item title styling.
  • Expanded regression test coverage for item titles, existing description references, explicit label targets and native label associations.
  • Corrected the remaining CSpell error without expanding the spelling dictionary further.

mgifford changed the visibility of the branch SOURCE_BRANCH to hidden.

benjifisher’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record, +Needs followup

@mgifford:

I reviewed the current MR with help from Claude.

The biggest problem is that the aria-describedby attribute is not added. I mentioned this in one of the MR comments. Back to NW. Or maybe that is on purpose: in Comment #46, you listed

Removed the additional aria-describedby association and generated title IDs.

as one of the changes. Is that what you meant? I thought a big part of this issue was to add the aria-describedby attribute.

Thanks for catching the CSS rules targeting .container-inline label. For testing purposes, I hacked `Drupal\system\Form\FileSystemForm`, adding some test form elements:

    $form['test_inline'] = [
      '#type' => 'item',
      '#title' => 'Inline item',
      '#markup' => 'Inline value',
      '#wrapper_attributes' => ['class' => ['container-inline']],
    ];
    $form['test_after'] = [
      '#type' => 'item',
      '#title' => 'Title after',
      '#title_display' => 'after',
      '#markup' => 'Value before title',
    ];
    $form['test_exposed'] = [
      '#type' => 'container',
      '#attributes' => ['class' => ['exposed-filters']],
      'item' => [
        '#type' => 'item',
        '#title' => 'Exposed filter item',
        '#markup' => 'Value',
      ],
    ];

Claude found some other places where label elements are targeted: see the MR comments and also core/misc/states.js, which toggles the form-required and js-form-required classes.

There are cases where the preprocess functions no longer pass a for attribute to the Twig template. This may be a problem for non-core themes that expect it. I am adding the issue tag for a change record.

As long as I am thinking about issue tags, I am adding one for a follow-up issue. In Comment #40, I changed the title and narrowed the scope of this issue. We should either restore the full scope or handle additional form elements in a separate issue.