Problem/Motivation

While working on the issue #1645328: Add test to ensure fieldset allows any non-empty-string #title I accidentally discovered this bug (see comments #55 and #57. According to @Liam Morland an empty legend tag should not be generated when the title of a fieldset is empty.

Steps to reproduce

Inside any .module file (from a currently active module on your site) paste the following code:

function [module name]_form_[form id]_alter(array &$form, FormStateInterface $form_state, $form_id) {
  $form['test_empty'] = [
    '#type' => 'fieldset',
    '#title' => '',
    '#description' => '',
  ];
  $form['test_full'] = [
    '#type' => 'fieldset',
    '#title' => 'Lorem ipsum',
    '#description' => 'Lorem ipsum',
  ];
}

Replace [module name] with the machine name of the module and [form id] with the id of any form you can "mess" with.
Clear the site cache
Access the form related to the form id you used previously.
You should see two new fields one empty one with "Loren ipsum" as the title and description.
Access the browser dev tools (usually pressing F12) ans inspect these new elements.
The empty field has a <legend> element inside.
Issue example image
Expected behavior: There should not be an empty title element (see the description field for comparison, when there's no description an empty description element is not created.)

Proposed resolution

Add an if condition to the title section(core/modules/system/templates/fieldset.html.twig)

  {% if legend.title %}
    <legend{{ legend.attributes }}>
      <span{{ legend_span.attributes.addClass(legend_span_classes) }}>{{ legend.title }}</span>
    </legend>
  {% endif %}

Before
Before

After
After

Remaining tasks

- Discuss the proposed solution
- Create a patch or MR with the solution + tests
- Review

Issue fork drupal-3283883

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

andregp created an issue. See original summary.

andregp’s picture

Priority: Normal » Minor

Updating priority.

andregp’s picture

Issue summary: View changes

fixing some markup.

jcbautista97’s picture

Status: Active » Needs review
StatusFileSize
new786 bytes

This is my contribution to solve the issue, please check.

I add the conditional to fieldset create the element title when is not empty.

Status: Needs review » Needs work

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

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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.

ranjith_kumar_k_u’s picture

Title: Fieldsed should not create title element when title is empty » Fieldset should not create title element when title is empty
Version: 9.5.x-dev » 10.1.x-dev
StatusFileSize
new1.3 KB
new2.17 KB

#4 provided the solution for the Seven theme, but the theme is no more with the core.

Claro and Olivero don't have this issue but the issue exists in "core/modules/system/templates/fieldset.html.twig" file.
Created patch with tests, please review

ranjith_kumar_k_u’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Can we also get a positive assertion. Meaning if the fieldset isn't empty and the title appears the count should be 1.

Rest looks good.

rpayanm made their first commit to this issue’s fork.

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.

ranjith_kumar_k_u’s picture

Status: Needs work » Needs review
StatusFileSize
new2.53 KB

Attached patch for 11.x.
(I tried to create an issue fork branch from 11.x but I couldn't, 3283883-11.x this is not the correct branch for 11.x please ignore this )

smustgrave’s picture

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

Issue summary could use some up to date screenshots.

Also since this is changing a core template will need a change record

ranjith_kumar_k_u’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new1.15 MB
new983.3 KB

Created a change record - https://www.drupal.org/node/3367586

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs screenshots, -Needs change record

Think this is ready for committer review.

lauriii’s picture

We need FEFM review to decide in which themes this should be fixed.

bnjmnm’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -Needs frontend framework manager review +Accessibility, +wcag332, +wcag131

Here's the FEFM / A11y review, but I regret to say it's not a clean response to what was being asked above.

It will likely be necessary to come at this from a different angle. The fact that it is currently possible to have empty <legend> elements is already not good for accessibility. Making it possible for <legend> to be truly optional worsens the accessibility.

To be level A accessible (level A is the bare minimum), fieldsets must have a legend as the first child element .

So something should be addressed, but not by allowing elements to be less accessible. These are the main things to consider

  • Olivero currently allows optional <legend>. This should be enforced
  • It would be best to enforce a non-empty #title for fieldsets. We can't suddenly start enforcing this in a non-major release, though, as it could break sites. Adding a deprecation warning in 10 for non-empty titles in 10 (possibly with an additional logged warning?) then enforcing in 11 could work.
bnjmnm’s picture

Issue summary will need updating given the wrench I just tossed into this.

kentr’s picture

@bnjmnm

It would be best to enforce a non-empty #title for fieldsets. We can't suddenly start enforcing this in a non-major release, though, as it could break sites. Adding a deprecation warning in 10 for non-empty titles in 10 (possibly with an additional logged warning?) then enforcing in 11 could work.

A quick experiment shows that we can probably do this by accommodating fieldset #933004: Test that all form elements have a #title for accessibility, which I'm working on.

I propose adding fieldset to the scope of that issue.

kentr’s picture

I think fieldset is already in scope of #933004: Test that all form elements have a #title for accessibility, really. It's a form element, after all. I'll go ahead and commit the experiment.

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.

kentr’s picture

mgifford’s picture

@kentr if fieldset is in scope of #933004: Test that all form elements have a #title for accessibility we should just list this status as postponed or even duplicate, right?