You are here

public function LayoutBuilderSampleEntityGenerator::get in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/layout_builder/src/Entity/LayoutBuilderSampleEntityGenerator.php \Drupal\layout_builder\Entity\LayoutBuilderSampleEntityGenerator::get()
  2. 9 core/modules/layout_builder/src/Entity/LayoutBuilderSampleEntityGenerator.php \Drupal\layout_builder\Entity\LayoutBuilderSampleEntityGenerator::get()

Gets a sample entity for a given entity type and bundle.

Parameters

string $entity_type_id: The entity type ID.

string $bundle_id: The bundle ID.

Return value

\Drupal\Core\Entity\EntityInterface An entity.

Overrides SampleEntityGeneratorInterface::get

File

core/modules/layout_builder/src/Entity/LayoutBuilderSampleEntityGenerator.php, line 44

Class

LayoutBuilderSampleEntityGenerator
Generates a sample entity for use by the Layout Builder.

Namespace

Drupal\layout_builder\Entity

Code

public function get($entity_type_id, $bundle_id) {
  $tempstore = $this->tempStoreFactory
    ->get('layout_builder.sample_entity');
  if ($entity = $tempstore
    ->get("{$entity_type_id}.{$bundle_id}")) {
    return $entity;
  }
  $entity_storage = $this->entityTypeManager
    ->getStorage($entity_type_id);
  if (!$entity_storage instanceof ContentEntityStorageInterface) {
    throw new \InvalidArgumentException(sprintf('The "%s" entity storage is not supported', $entity_type_id));
  }
  $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;
}