function LayoutBuilderSampleEntityGenerator::get

Same name in other branches
  1. 8.9.x core/modules/layout_builder/src/Entity/LayoutBuilderSampleEntityGenerator.php \Drupal\layout_builder\Entity\LayoutBuilderSampleEntityGenerator::get()
  2. 10 core/modules/layout_builder/src/Entity/LayoutBuilderSampleEntityGenerator.php \Drupal\layout_builder\Entity\LayoutBuilderSampleEntityGenerator::get()
  3. 11.x core/modules/layout_builder/src/Entity/LayoutBuilderSampleEntityGenerator.php \Drupal\layout_builder\Entity\LayoutBuilderSampleEntityGenerator::get()

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.