Problem/Motivation
On Drupal 11.3.18 with PHP 8.5, saving and viewing content with Layout Builder enabled but no section selected produces repeated deprecation warnings:
Deprecated function: Using null as an array offset is deprecated, use an empty string instead in Drupal\layout_builder\Section->toRenderArray()
The empty Layout Builder state uses the internal layout_builder_blank layout. Its retained components have no region because no real section and therefore no region has yet been selected.
Section::toRenderArray() nevertheless renders those components and uses the null region as an array key:
$regions[$component->getRegion()][$component->getUuid()] = $output;
Adding a real section gives the components valid regions and stops the warnings. Configuring a default section is not a general workaround because doing so preselects a layout before the content creator chooses the required layout.
Diagnostic logging produced entries such as:
Layout Builder component 6876ecee-c2cc-46b6-99c4-321974d8852d in layout layout_builder_blank has no region.
While the problem has not been reproduced beyond Drupal 11.3.18, 11.4.x still uses the same Section::toRenderArray() logic, and no 11.4.x release note or committed issue fixing this Layout Builder path was found. Drupal 11.4.6 included other PHP 8.5 deprecation fixes, but not this one.
Steps to reproduce
- Use Drupal latest stable or main with PHP 8.5.
- Install standard profile
- Login and add a content type
- Go to manage display for the content type, enable Layout builder on the Default display, click Manage layout and delete the default section, this will make the display fall back to a layout_builder_blank section
- Add a field to the content type - this will cause the new field to be added to the blank section (this will change in #3115759: Prevent auto-adding new fields to LB layouts)
- Create a node of that type.
- Save the content.
- Observe repeated deprecation warnings from Section::toRenderArray().
- Add a real section and save again.
- Observe that the warnings stop.
Proposed resolution
Guard against NULL regions.
| Comment | File | Size | Author |
|---|---|---|---|
| Issue with no sections yet - example 2.png | 117.05 KB | seeduardo | |
| Issue with no sections yet - example.png | 143.8 KB | seeduardo |
Issue fork drupal-3629628
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
Comment #4
seeduardo commentedI added an initial fix that returns an empty render array for the internal
layout_builder_blankplaceholder before its regionless components are rendered.Comment #5
acbramley commentedI just hit this as well, I wonder if it'd be better to just
continueinside the loop if$component->getRegion()is NULL?Comment #6
smustgrave commentedNot sure I follow "Leave the overridden layout without a selected section." could the steps be cleaned up some?
Comment #7
acbramley commentedComment #8
acbramley commentedI've pushed a slightly different solution which I think is more robust and added a test.
Comment #9
smustgrave commentedSweet I’ll test it out tomorrow. I wasn’t sure we needed a test or not
Comment #10
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #11
acbramley commented