Drupal entities containing optional or multiple fields do not render patterns correctly

Created on 17 August 2023, about 1 year ago
Updated 26 August 2023, about 1 year ago

Hello,
I have a question about multiple and optional fields that seems to be related to the issue #3342390 Make the Twig loops safer Postponed on UI Patterns module.

In the case of DSFR, many fields among the patterns are optionals or multiples.

However, HTML tags are still rendered for optional fields, and markup is not correctly generated for multiple fields.

For example, I created a paragraph tile and 3 fields (Image displayed as a thumbnail, title as plain text and description as plain text) for the tile pattern. I add pattern tile in display mode (settings : only content)

In my case, I didn't fill in the image and description fields, so I should have this rendering:

<div class="fr-tile--horizontal fr-enlarge-link fr-tile">
    <div class="fr-tile__body">
      <h4 class="fr-tile__title">Title tile lorem ipsum</h4>
    </div>
</div> 

or the pattern renders :

<div class="fr-tile--horizontal fr-enlarge-link fr-tile">
    <div class="fr-tile__body">
      <h4 class="fr-tile__title">
        Titre tuile 2
      </h4>
      <p class="fr-tile__desc"></p>
    </div>
    <div class="fr-tile__img"></div>
</div>

This impacts the alignment of fields, and their rendering.
I wanted to know if a solution was envisaged to avoid each project overloading all the patterns concerned by UI Suite DSFR while waiting for this comment https://www.drupal.org/project/ui_patterns/issues/3342390#comment-15037218 Make the Twig loops safer Postponed to be processed.

Thank you in advance and have a nice day
Marc

🐛 Bug report
Status

Closed: won't fix

Version

1.0

Component

User interface

Created by

🇷🇪Réunion Martygraphie Saint-Denis (Réunion)

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Martygraphie
  • Status changed to Postponed: needs info about 1 year ago
  • 🇫🇷France pdureau Paris

    Hi Marc-Antoine,

    There are 2 distinct topics in your ticket.

    Looping on slots (fields) values

    As you already know, it is the scope of Make the Twig loops safer Postponed and we hope it will be fixed upstream, at Twig level.

    Testing if a render array is true or false

    It is a huge issue in Drupal https://www.drupal.org/project/drupal/issues/953034 🌱 [meta] Themes improperly check renderable arrays when determining visibility Needs work a nightmare for some, but it was not a big deal with UI Patterns usually. However, your issue seems related to this, so let's investigate.

    Nothing wrong with the component itself. And nothing wrong with ui_suite_dsfr. It is about the render API.

    Let's do some tests with this slot:

    {% if description %}<p class="fr-tile__desc">{{ description }}</p>{% endif %}

    From https://git.drupalcode.org/project/ui_suite_dsfr/-/blob/1.0.x/templates/...

    We expect description to be false.

    Test 1: empty array

    $variables['description'] = [];

    OK: description is false, because in PHP an empty array is false

    Test 2: non renderable properties

    [
      "#cache" => [
        "contexts" => [
          "user.permissions"
        ],
        "tags" => [],
        "max-age" => -1
      ]
    ]

    OK: description is false. i don't know which mechanism do that, but it works OK

    Test 3: non renderable properties wrapped in a mapping

    Layout Builder is sometimes wrapping the render array in a mapping where the key is the block UUID:

    [
    "cf124739-3274-41cc-829d-c3465ee4eaa5" => [
      "#cache" => [
        "contexts" => [
          "user.permissions"
        ],
        "tags" => [],
        "max-age" => -1
      ]
    ]
    ]

    KO: description is true. it must be false because there are only non renderable properties here. it seems Drupal is only testing the first level

    Marc-Antoine, you tested with Display Suite, so it was another wrapping with another key (the field name, i guess), but it was the same issue. What do you think about this?

  • 🇷🇪Réunion Martygraphie Saint-Denis (Réunion)

    Hi Pierre,
    Yes, I reproduce test 4 (Test 4: non renderable properties wrapped in a mapping) with Display Suite / Display Suite Ui Patterns. But instead of having the uuid generated by Layout Builder, I have the machine name of the field.

  • Assigned to pdureau
  • Status changed to Needs work about 1 year ago
  • Issue was unassigned.
  • Status changed to Closed: won't fix about 1 year ago
  • 🇫🇷France pdureau Paris

    This issue is not specific to DSFR and has been moved elsewhere: Empty field values when not renderable Active

Production build 0.71.5 2024