Problem/Motivation

SampleEntityGenerator::get() fills the sample entity with core's createWithSampleValues(), and a site cannot change those values.

A 255 character string field gets 26 to 254 characters, so a short text often wraps to two lines, and a long text field gets plain paragraphs with no markup.

Display Builder renders its entity view and preset previews from this entity, so the random values are what site builders design against.

Steps to reproduce

  1. Add a plain text field to the article content type, and use it with the body field in an article display or preset of Display Builder.
  2. The Canvas shows that field as one or two random lines and the body as unformatted paragraphs.

Proposed resolution

When field_sample_value is installed, get() passes the new entity to its populateWithSampleValues() before storing it in the tempstore.
It only touches fields with a generator configured, so the other fields keep core values, and sites without the module see no change.

The service is optional, so UI Patterns gets no new dependency.

Layout Builder+ also integrates field_sample_value.

A field config update now clears the cached sample, as a delete already does, or a newly configured generator only shows once the tempstore entry expires.
Base fields such as the node title stay on core values: field_sample_value only configures field config entities.

Remaining tasks

  • Review.

User interface changes

None in UI Patterns. Generators are set on the field edit form, by field_sample_value.

API changes

SampleEntityGenerator takes an optional third constructor argument, the field_sample_value generator service.

Data model changes

None.

Command icon 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

mogtofu33 created an issue. See original summary.

harrytrivedi changed the visibility of the branch 3631122-field-sample-value to hidden.

harrytrivedi changed the visibility of the branch 3631122-field-sample-value to active.

mogtofu33’s picture

Issue summary: View changes
mogtofu33’s picture

just_like_good_vibes’s picture

I would rather suggest to move this subject to core's SampleEntityGenerator

mogtofu33’s picture

Assigned: mogtofu33 » Unassigned
Status: Needs work » Needs review

Agreed core is the right long term home, but that path is much longer: the values come from ContentEntityStorageBase::createWithSampleValues(), which calls each field type's static generateSampleValue() with no hook or event after it, so core needs a new API first.
And core's generator, LayoutBuilderSampleEntityGenerator, lives inside Layout Builder, which is why UI Patterns ships its own copy, so a change there would not reach UI Patterns anyway.

Meanwhile this MR costs nothing: the service is optional, there is no new dependency, and sites without field_sample_value see no change.

This matters for Display Builder, the core defaults make previews feel broken, so if it is postponed here I will carry it as a patch there.

Happy to open the core issue in parallel and switch to it once it lands.