Problem/Motivation

If you use Grid Row component in a view and you have only one result, you will get this fatal error:

Exception: Object of type Drupal\node\Entity\Node cannot be printed. in Drupal\Core\Template\TwigExtension->escapeFilter()

Because content is a single renderable, so we are looping on the renderable properties:

  {% for item in content %}
    {% set col_sizes = [col_xs, col_sm, col_md, col_lg, col_xl, col_xxl, spacing_margin_bottom] %}
    <div{{ create_attribute({'class': col_sizes}) }}>
      {{ item }}
    </div>
  {% endfor %}

Proposed resolution

Since Twig 3.11, there are 2 new tests: is sequence and is mapping, so we can use them to check if there is a single renderable and wrap it into a sequence before looping:

  {% set content = content is sequence ? content : [content] %}

However, we may need to wait the move to 5.1.x branch because the 5.0.x branch is still supporting Drupal 10.2:

This change need to be done everywhere there is a loop on a slot:

carousel/carousel.twig:    {% for slide in slides %}
grid_row/grid_row.twig:  {% for item in content %}
nav/nav.twig:    {% for pane in tab_content %}
table/table.twig:  {% for stripe in stripes %}
table/table.twig:  {% for colgroup in colgroups %}
table/table.twig:        {% for col in colgroup.cols %}

What else?

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

pdureau created an issue. See original summary.

grimreaper’s picture

Assigned: Unassigned » grimreaper
grimreaper’s picture

grimreaper’s picture

stripes in table is a prop.

grimreaper’s picture

Assigned: grimreaper » pdureau
Status: Active » Needs review
grimreaper’s picture

Title: Use "is sequence" Twig test to prevent fatal errors when looping on slots » [5.1.x] Use "is sequence" Twig test to prevent fatal errors when looping on slots
Assigned: pdureau » grimreaper
grimreaper’s picture

Title: [5.1.x] Use "is sequence" Twig test to prevent fatal errors when looping on slots » Use "is sequence" Twig test to prevent fatal errors when looping on slots
Version: 5.0.x-dev » 5.1.x-dev
grimreaper’s picture

Assigned: grimreaper » Unassigned
Status: Needs review » Fixed

  • grimreaper committed 54284d21 on 5.1.x
    Issue #3484746 by grimreaper, pdureau: Use "is sequence" Twig test to...

Status: Fixed » Closed (fixed)

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