lazy builders that return an element with a #type property fail silently

Created on 17 January 2021, over 4 years ago
Updated 20 February 2025, 3 months ago

Problem/Motivation

Lazy builders can't return a single element with a #type property, because of 🐛 Lazy builder broken (#type defaults not loaded) Needs work .

This is documented at https://www.drupal.org/docs/8/api/render-api/auto-placeholdering , but I've been bitten by it twice.

When you do return a single element with a #type property, the lazy builder output is simply not shown and there's no explanation.

Steps to reproduce

Return a single element from a lazy builder callback that uses a #type.

Proposed resolution

Throw an exception in Renderer::doRender() to explain what's going wrong:

      if (isset($new_elements['#type'])) {
        throw new \LogicException('The result of a #lazy_builder callback may not be a #type element. Wrap a #type element in an array as a child. See https://www.drupal.org/project/drupal/issues/2609250.');
      }

Remaining tasks

Are there some values of #type that DO currently work, and whose behaviour should be preserved? I don't understand what '#type defaults not loaded' means on that other issue.

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

🐛 Bug report
Status

Closed: duplicate

Version

11.0 🔥

Component

render system

Created by

🇬🇧United Kingdom joachim

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024