Allow generateSampleValue(s) to fail

Created on 12 April 2021, about 3 years ago
Updated 10 June 2023, about 1 year ago

Problem/Motivation

It is assumed that every field type is able to generate sample values, via:
- FieldItemInterface::generateSampleValue().
- FieldItemListInterface::generateSampleValues().

Some field types already have cases where they return an empty array in generateSampleValue().
There can also be dynamic conditions where a sample value cannot be generated.
Also, there is no guarantee that field types in contrib or custom modules generate non-empty sample values.

And yet it seems that calling code completely ignores the case where sample values are not properly generated.

Steps to reproduce

Create a custom field type where generateSampleValue() returns empty array, or something else.

A more common case:
- Ceate a reference field (in my case it was entity_reference_revisions with paragraphs).
- Remove all bundles that were allowed in the field
- Visit a layout builder page. Layout builder attempts to fill regions with default content for preview, and will crash if a field fails to provide a sample value.

Proposed resolution

Clarify the signature of generateSampleValue(), describing what a method should do if no sample value cannot be created.

Let calling code detect empty or broken sample values returned from field types, and catch exceptions.

Implement fallback behavior and reporting.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Closed: duplicate

Version

11.0 πŸ”₯

Component
FieldΒ  β†’

Last updated about 1 hour ago

Created by

πŸ‡©πŸ‡ͺGermany donquixote

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.69.0 2024