Fieldset should not create title element when title is empty

Created on 2 June 2022, over 2 years ago
Updated 22 June 2023, over 1 year ago

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.

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

After

Remaining tasks

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

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Form 

Last updated 2 days ago

Created by

🇧🇷Brazil andregp

Live updates comments and jobs are added and updated live.
  • Accessibility

    It affects the ability of people with disabilities or special needs (such as blindness or color-blindness) to use Drupal.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024