Allow empty array in SDC slots

Created on 22 May 2025, 21 days ago

Problem/Motivation

If I set a single-directory component slot to an empty array, I will get this error:

Drupal\Core\Render\Component\Exception\InvalidComponentDataException: Unable to render component "MODULE:COMPONENT". A render array or a scalar is expected for the slot "SLOT" when using the render element with the "#slots" property in Drupal\Core\Render\Element\ComponentElement->generateComponentTemplate() (line 118 of /.../core/lib/Drupal/Core/Render/Element/ComponentElement.php).

Setting empty values to NULL also does not work. If a slot of empty, it has to be not set.

Proposed resolution

Allow empty array in slots. An empty Drupal render array should render to the empty string. The fix will require changing Drupal\Core\Render\Element::isRenderArray().

Remaining tasks

Agree on what to do.

User interface changes

None.

Introduced terminology

None.

API changes

Empty arrays will be allowed as the value for SDC slots.

Data model changes

None.

Release notes snippet

🐛 Bug report
Status

Active

Version

11.0 🔥

Component

single-directory components

Created by

🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

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

Merge Requests

Comments & Activities

  • Issue created by @Liam Morland
  • 🇪🇸Spain idiaz.roncero Madrid

    Agree that this is a bug. Especially if you consider that using an empty array using components directly on twig, you get no error at all: this only happens when using the RenderElement on your code.

    There should be no difference on the accepted input values between the two ways to "invoke" a component.

  • 🇪🇸Spain idiaz.roncero Madrid

    Created a simple MR that allows empty arrays to bypass the negative check from Element::isRenderArray.

    This is a solution, but it might be controversial and is no small change as it assumes that,contrary to what Drupal usually does, an empty array will be considered a valid render array in this particular context.

    A different solution could be the opposite: making the twig embed and includes of components apply the same logic and disallow passing empty arrays as slot values (right now, they swallow it)

    What we have now - different behavior depending on the way a component is called - is misleading. We need to chose one direction or the other.

    (I am obviously not proposing here to let developers pass empty arrays voluntarily, but you can end up with an empty array as an unintended result of some business logic, both on twig and on PHP code, and we need to decide if this is something we want to support or not).

  • Pipeline finished with Failed
    1 day ago
    Total: 564s
    #519824
  • 🇨🇦Canada Liam Morland Ontario, CA 🇨🇦

    Fix typo.

Production build 0.71.5 2024