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