{{ item.attributes }} is not used when label is hidden and its a single item

Created on 10 August 2018, almost 7 years ago
Updated 9 June 2025, about 1 month ago

Problem/Motivation

When there is only one item in the field, and the label is hidden the item.attributes is not used. If we want to add attributes from hook_preprocess_HOOK() we need to handle both cases, add attributes if several items to all items from $variables['items'][$index]['attributes] or when there is only one item, we have to add class to $variables['attributes'].

File path : core/modules/system/templates/field.html.twig
Code line : 51

{% if label_hidden %}
  {% if multiple %}
    <div{{ attributes }}>
      {% for item in items %}
        <div{{ item.attributes }}>{{ item.content }}</div>
      {% endfor %}
    </div>
  {% else %}
    {% for item in items %}
      <div{{ attributes }}>{{ item.content }}</div>
    {% endfor %}
  {% endif %}
{% else %}

Proposed resolution

Remove the {% else %} condition in the {% if label_hidden %} and keep the implementation from {% if multiple %}.

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component

system.module

Created by

🇫🇷France xavier.masson Haute-Normandie

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

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