You are here

class LayoutBuilderSampleEntityGenerator 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
  2. 9 core/modules/layout_builder/src/Entity/LayoutBuilderSampleEntityGenerator.php \Drupal\layout_builder\Entity\LayoutBuilderSampleEntityGenerator

Generates a sample entity for use by the Layout Builder.

Hierarchy

Expanded class hierarchy of LayoutBuilderSampleEntityGenerator

1 string reference to 'LayoutBuilderSampleEntityGenerator'
layout_builder.services.yml in core/modules/layout_builder/layout_builder.services.yml
core/modules/layout_builder/layout_builder.services.yml
1 service uses LayoutBuilderSampleEntityGenerator
layout_builder.sample_entity_generator in core/modules/layout_builder/layout_builder.services.yml
Drupal\layout_builder\Entity\LayoutBuilderSampleEntityGenerator

File

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

Namespace

Drupal\layout_builder\Entity
View source
class LayoutBuilderSampleEntityGenerator implements SampleEntityGeneratorInterface {

  /**
   * The shared tempstore factory.
   *
   * @var \Drupal\Core\TempStore\SharedTempStoreFactory
   */
  protected $tempStoreFactory;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * LayoutBuilderSampleEntityGenerator constructor.
   *
   * @param \Drupal\Core\TempStore\SharedTempStoreFactory $temp_store_factory
   *   The tempstore factory.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(SharedTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $entity_type_manager) {
    $this->tempStoreFactory = $temp_store_factory;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LayoutBuilderSampleEntityGenerator::$entityTypeManager protected property The entity type manager.
LayoutBuilderSampleEntityGenerator::$tempStoreFactory protected property The shared tempstore factory.
LayoutBuilderSampleEntityGenerator::delete public function Deletes a sample entity for a given entity type and bundle. Overrides SampleEntityGeneratorInterface::delete
LayoutBuilderSampleEntityGenerator::get public function Gets a sample entity for a given entity type and bundle. Overrides SampleEntityGeneratorInterface::get
LayoutBuilderSampleEntityGenerator::__construct public function LayoutBuilderSampleEntityGenerator constructor.