Classy field wrong structure

Created on 11 August 2020, over 4 years ago
Updated 9 April 2025, 8 days ago

Hi, I notice that in Classy theme, the field.html.twig has some issue.

Here is the ref from the drupal core repo for field.html.twig

First of all, in the first "if" part of code we can find

{% if label_hidden %}
  {% if multiple %}
    <div{{ attributes.addClass(classes, 'field__items') }}>
      {% for item in items %}
        <div{{ item.attributes.addClass('field__item') }}>{{ item.content }}</div>
      {% endfor %}
    </div>
  {% else %}
    {% for item in items %}
      <div{{ attributes.addClass(classes, 'field__item') }}>{{ item.content }}</div>
    {% endfor %}
  {% endif %}
{% else %}

in the second section of this part of code we can find

for item in items

but the div inside has

attributes.addClass(classes, 'field__item')

instead of

item.attributes.addClass(classes, 'field__item')

as it should be and as stated here.

going to the second part of this code we find

{% else %}
  <div{{ attributes.addClass(classes) }}>
    <div{{ title_attributes.addClass(title_classes) }}>{{ label }}</div>
    {% if multiple %}
      <div class="field__items">
    {% endif %}
    {% for item in items %}
      <div{{ item.attributes.addClass('field__item') }}>{{ item.content }}</div>
    {% endfor %}
    {% if multiple %}
      </div>
    {% endif %}
  </div>
{% endif %}

and to conclude, in the first part (the "if label_hidden" one) we have a container div with attributes classes only on fields with multiple configuration. In the second part, we have that container div for all our fields and inside this one we have a new container div with "field__items" class (another markup change based on context but probably not necessary).

why first and second part are so different? the markup can be changed to be more consistant.

I have created a patch with a cleaner structure that should work and should be more consistent. (I will attach the patch to the comment below).

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇮🇹Italy ralkeon

Live updates comments and jobs are added and updated live.
  • 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