LayoutBuilderSampleEntityGenerator.php in Drupal 8
File
core/modules/layout_builder/src/Entity/LayoutBuilderSampleEntityGenerator.php
View source
<?php
namespace Drupal\layout_builder\Entity;
use Drupal\Core\Entity\ContentEntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\TempStore\SharedTempStoreFactory;
class LayoutBuilderSampleEntityGenerator implements SampleEntityGeneratorInterface {
protected $tempStoreFactory;
protected $entityTypeManager;
public function __construct(SharedTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $entity_type_manager) {
$this->tempStoreFactory = $temp_store_factory;
$this->entityTypeManager = $entity_type_manager;
}
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);
$entity->in_preview = TRUE;
$tempstore
->set("{$entity_type_id}.{$bundle_id}", $entity);
return $entity;
}
public function delete($entity_type_id, $bundle_id) {
$tempstore = $this->tempStoreFactory
->get('layout_builder.sample_entity');
$tempstore
->delete("{$entity_type_id}.{$bundle_id}");
return $this;
}
}