The default content importer should handle Layout Builder section data

Created on 11 December 2024, 10 days ago

Problem/Motivation

Drupal CMS is going to need the ability to ship default content that includes layout section data. You can export such content from the Default Content module, provided you have ✨ Add a Normalizer and Denormalizer to support Layout Builder RTBC applied to it, but you cannot import it using \Drupal\drupal_cms_polyfills\Importer because it doesn't know how to handle section data that has been downcast to an array, even though Layout Builder itself explicitly supports converting section data to and from arrays.

Steps to reproduce

Apply the aforementioned patch to default_content, and export a node (or anything) that contains a layout section. The export will succeed. Put that exported content into a recipe and try to apply the recipe, and it will fail with this error: Value assigned to "section" is not a valid section .

Proposed resolution

Add the following three lines to \Drupal\Core\DefaultContent\Importer::setFieldValues():

        elseif ($property instanceof SectionData && is_array($value)) {
          $value = Section::fromArray($value);
        }

This is a hack for sure, and it means that the core DefaultContent subsystem will be referencing core module code, which kind of sucks, but this elseif branch should never even be reachable unless Layout Builder is enabled (without it, no typed data property could possibly be an instance of SectionData).

In a follow-up, we need to move all this per-field logic into some kind of normalization structure that the importer can make use of, rather than needing to handle different data types all in one place.

Another option, proposed by @longwave in Slack, is for SectionData::setValue() to allow arrays, and call Section::fromArray(). However, it has code to explicitly reject any value that is not already a Section object, and the reason why is not clear; that would definitely be a preferable solution, but it would need input from the Layout Builder maintainers.

Remaining tasks

Implement with test coverage, and land this in 11.1.0-rc2. In Slack, @catch felt (informally) that this could land in RC as an internal change to an experimental subsystem.

User interface changes

None.

Introduced terminology

None.

API changes

None.

Data model changes

None.

Release notes snippet

TBD, probably none.

✨ Feature request
Status

Active

Version

11.0 πŸ”₯

Component

recipe system

Created by

πŸ‡ΊπŸ‡ΈUnited States phenaproxima Massachusetts

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024