False positive when checking empty slots

Created on 10 May 2025, 2 months ago

Problem/Motivation

In UI Suite Bootstrap, in the table component, there is:

  {% if caption %}
    <caption>{{ caption }}</caption>
  {% endif %}

The table presenter template is passing an empty string as the caption variable.

But in the component template I have an array with an empty #children.

In ui_patterns/src/Plugin/UiPatterns/PropType/SlotPropType.php, there are 2 problematic places :

    if (is_string($value)) {
      return ['#children' => Markup::create($value)];
    }

After making the proposed change I obtain an empty array with #cache from:

protected static function cleanRenderArray(array $value): mixed {
    if (empty($value)) {
      // Element::isRenderArray() returns FALSE for empty arrays.
      return ['#cache' => []];
    }

Steps to reproduce

Using UI Suite Bootstrap, create a View using a table display.

Proposed resolution

    if (is_string($value) && !empty($value)) {
      return ['#children' => Markup::create($value)];
    }

I don't know what to do about the other place where there is a problem.

Remaining tasks

πŸ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·

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

Merge Requests

Comments & Activities

  • Issue created by @Grimreaper
  • πŸ‡«πŸ‡·France pdureau Paris
  • πŸ‡ΊπŸ‡¦Ukraine SmovS Lutsk
  • Merge request !386#3523756 - Added a check on not empty value. β†’ (Open) created by SmovS
  • Pipeline finished with Success
    about 2 months ago
    Total: 534s
    #505584
  • πŸ‡ΊπŸ‡¦Ukraine SmovS Lutsk

    Hi @grimreaper
    I have added a check for a non-empty value according to your proposal.

    But I'm still confused about this false positive when checking slots.
    E.g. I set the caption in the table with wysiwig source with an empty textarea.

    In this case, the table contains an empty caption tag because wysiwig source creates an array with text format settings.
    It would be cleaned by self::cleanRenderArray(), but didn't

    I am not sure if it is related to the false positive that you mentioned. Probably I went the wrong way.

  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·

    Hi @smovs,

    The problem occurs for example when using the table component with the views table presenter template.

    If you are using a component "manually" or with site building it is ok.

    But with a presenter template, like written in the issue summary, you can get an empty string as slot value and in this case the problem occurs.

    So, to test you can create a view using the table display (not component display using the table component).

  • πŸ‡«πŸ‡·France pdureau Paris
  • πŸ‡ΊπŸ‡¦Ukraine SmovS Lutsk

    Hi @grimreaper

    Sorry for the delay.

    What I found.
    From my point of view, rendering works as expected in general, because:
    When we check if $value is empty and set it ['#cache' => []], we create a non-empty array. It means we redefine the variable from non-array to array in this case. See SlotPropType::normalize

        if (!is_array($value)) {
          return empty($value) ? ['#cache' => []] : ['#plain_text' => (string) $value];
        }

    In Twig template, when we check {% if caption %} is always true because in this case, we pass a non-empty array.

    I am not fully understanding the purpose of the forced setting ['#cache' => []] each time.

    I added screenshots for better understanding

  • πŸ‡«πŸ‡·France pdureau Paris
  • πŸ‡«πŸ‡·France Grimreaper France πŸ‡«πŸ‡·

    Hi @smovs,

    In Twig template, when we check {% if caption %} is always true because in this case, we pass a non-empty array.

    Yes, and that's the purpose of this issue, we should be able to keep a simple if statement in Twig and having check as expected. We don't want an empty caption to appear.

    we create a non-empty array.

    That's what needs to be challenged.

    Note: I can't reassign the issue to you.

  • πŸ‡ΊπŸ‡¦Ukraine SmovS Lutsk

    @grimreaper
    Thank you for the clarification. I will take a look on this weekend

  • Pipeline finished with Failed
    2 days ago
    Total: 2471s
    #544932
  • Pipeline finished with Failed
    2 days ago
    Total: 285s
    #544977
  • Pipeline finished with Failed
    2 days ago
    Total: 220s
    #544982
  • Pipeline finished with Canceled
    2 days ago
    Total: 91s
    #544988
  • Pipeline finished with Failed
    2 days ago
    Total: 678s
    #544989
Production build 0.71.5 2024