EntityReferenceRevisionsItem::generateSampleValue() saves entities, ignoring the Tempstore

Created on 8 May 2025, 7 days ago

Problem/Motivation

I have looked at other issues, and this seems unique, at least as I define it.

When using Layout Builder with a block that contains an ERR field, referenced entities are created and saved as if they were "real" content, rather than saving to tempstore.

Here's the call stack summary:

* LayoutBuilderSampleEntityGenerator::get() triggers entity creation
* Entities and saved in tempstore 
	$tempstore = $this->tempStoreFactory->get('layout_builder.sample_entity');
	$entity = $entity_storage->createWithSampleValues($bundle_id);
	// Mark the sample entity as being a preview.
    $entity->in_preview = TRUE;
    $tempstore->set("$entity_type_id.$bundle_id", $entity);
    return $entity;
* This routine calls ContentEntityStorageBase::createWithSampleValues() 
* That invokes $entity->get($field_name)->generateSampleItems();
* That calls FieldItemList::generateSampleItems()
	$values[$delta] = $field_type_class::generateSampleValue($field_definition);
* Which invokes EntityReferenceRevisionsItem::generateSampleValue()
* Which calls an $entity_save() that ignore the tempstore.	

The result are entities that are created and saved when Layout Builder is loaded.

Steps to reproduce

  • Create a block that has an ERR field to reference a content type (say, Basic page).
  • Add that block reference as an ERR field on the Article type
  • Enable Layout Builder for the Article type
  • Open the Layout Builder
  • Go to admin/content and see that a Basic Page has been created

Proposed resolution

Use the proper tempstore for entities created during generateSampleValue()

Remaining tasks

Rew

  1. ire the generateSampleValue() method
  2. Test

User interface changes

None

API changes

None

Data model changes

None

πŸ› Bug report
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)

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

Comments & Activities

Production build 0.71.5 2024