Using the ui_patterns view style can error out when there is a single result row

Created on 13 May 2025, about 2 months ago

Problem/Motivation

When you configure a view to use a pattern as view style, and map the 'View rows' to a slot. You can get an error like

Exception: Object of type Drupal\commerce_product\Entity\Product cannot be printed. in Drupal\Core\Template\TwigExtension->escapeFilter() (line 459 of core/lib/Drupal/Core/Template/TwigExtension.php).

I found that function isSingletonRenderArray wrongfully removes the array surrounding the single views result row.

In our pattern, we loop over the slot to which the 'View rows' are mapped, resulting in the mentioned error.

Steps to reproduce

  1. Configure a view and use a pattern as view style
  2. Map the 'View rows' to a slot
  3. Add a filter that gives you a single result
  4. Go the view and use the filter

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡§πŸ‡ͺBelgium stefdewa

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

Merge Requests

Comments & Activities

  • Issue created by @stefdewa
  • πŸ‡«πŸ‡·France pdureau Paris
  • πŸ‡§πŸ‡ͺBelgium stefdewa

    FYI, our workaround in our pattern is wrapping "grid_items" if it isn't a sequence.

    <ul class="{{ component ~ '__grid-items' }} {{ component ~ '__grid-items--' ~ variant }}">
      {% set grid_items = grid_items and grid_items is not sequence ? [grid_items] : grid_items %}
      {% for grid_item in grid_items %}
        <li class="{{ component ~ '__grid-item' }}">{{ grid_item }}</li>
      {% endfor %}
    </ul>
    
  • πŸ‡©πŸ‡ͺGermany Christian.wiedemann

    christian.wiedemann β†’ made their first commit to this issue’s fork.

  • Merge request !394Add check β†’ (Merged) created by Christian.wiedemann
  • πŸ‡©πŸ‡ͺGermany Christian.wiedemann

    I updated the check for multi value slots.

  • πŸ‡«πŸ‡·France pdureau Paris
  • First commit to issue fork.
  • πŸ‡«πŸ‡·France just_like_good_vibes PARIS

    Hello,
    this bug is also appearing in another particular context :
    you define a slot, which should implicitly be an array of items. It is used in twig as an array of items.

    When you map - with the component form of ui patterns - some data into this particular slot (in my case with a [Entity]->referended [Entity] source), you can be blocked because when you save the form, the sample entity may contains only one value, so you are injecting an array of size one, but that array is then reduced by the code pinpointed in that issue. And then the twig of component is mis-behaving, treating a single item as an array of items... and exception could occur, preventing to save the component form!
    So another reason to push this fix.

    As we discussed with Christian, we made the choice to keep the weird code already there but we added more control before apply the change, this allows to avoid edge cases.

    and the fix is working for the case i just described here, also the case described by christian.

    To say more about the weird code i mentioned, it is helping to inject arrays in slots. Indeed, sometimes you want slot to be of a certain "format", in other words you will use a slot as a container for a sequence of items (renderables). To inject an array with UI, you usually use component form, you select a source to be used for the first item in a slot, and that source is returning an array of values. Users usually expect that array to be injected in the slot and not [0 => the_array] as it is naturally the case. So because slot values are render arrays, we reduce single keyed-render arrays to allow the array to be at root level. This is done without loosing rendering information. But unfortunately we were not covering well all possible cases. now it's better.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024