Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / layout_builder / src / Entity / LayoutBuilderSampleEntityGenerator.php
1 <?php
2
3 namespace Drupal\layout_builder\Entity;
4
5 use Drupal\Core\Entity\ContentEntityStorageInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\TempStore\SharedTempStoreFactory;
8
9 /**
10  * Generates a sample entity for use by the Layout Builder.
11  *
12  * @internal
13  *   Layout Builder is currently experimental and should only be leveraged by
14  *   experimental modules and development releases of contributed modules.
15  *   See https://www.drupal.org/core/experimental for more information.
16  */
17 class LayoutBuilderSampleEntityGenerator {
18
19   /**
20    * The shared tempstore factory.
21    *
22    * @var \Drupal\Core\TempStore\SharedTempStoreFactory
23    */
24   protected $tempStoreFactory;
25
26   /**
27    * The entity type manager.
28    *
29    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
30    */
31   protected $entityTypeManager;
32
33   /**
34    * LayoutBuilderSampleEntityGenerator constructor.
35    *
36    * @param \Drupal\Core\TempStore\SharedTempStoreFactory $temp_store_factory
37    *   The tempstore factory.
38    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
39    *   The entity type manager.
40    */
41   public function __construct(SharedTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $entity_type_manager) {
42     $this->tempStoreFactory = $temp_store_factory;
43     $this->entityTypeManager = $entity_type_manager;
44   }
45
46   /**
47    * Gets a sample entity for a given entity type and bundle.
48    *
49    * @param string $entity_type_id
50    *   The entity type ID.
51    * @param string $bundle_id
52    *   The bundle ID.
53    *
54    * @return \Drupal\Core\Entity\EntityInterface
55    *   An entity.
56    */
57   public function get($entity_type_id, $bundle_id) {
58     $tempstore = $this->tempStoreFactory->get('layout_builder.sample_entity');
59     if ($entity = $tempstore->get("$entity_type_id.$bundle_id")) {
60       return $entity;
61     }
62
63     $entity_storage = $this->entityTypeManager->getStorage($entity_type_id);
64     if (!$entity_storage instanceof ContentEntityStorageInterface) {
65       throw new \InvalidArgumentException(sprintf('The "%s" entity storage is not supported', $entity_type_id));
66     }
67
68     $entity = $entity_storage->createWithSampleValues($bundle_id);
69     // Mark the sample entity as being a preview.
70     $entity->in_preview = TRUE;
71     $tempstore->set("$entity_type_id.$bundle_id", $entity);
72     return $entity;
73   }
74
75   /**
76    * Deletes a sample entity for a given entity type and bundle.
77    *
78    * @param string $entity_type_id
79    *   The entity type ID.
80    * @param string $bundle_id
81    *   The bundle ID.
82    *
83    * @return $this
84    */
85   public function delete($entity_type_id, $bundle_id) {
86     $tempstore = $this->tempStoreFactory->get('layout_builder.sample_entity');
87     $tempstore->delete("$entity_type_id.$bundle_id");
88     return $this;
89   }
90
91 }