X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fcore%2Fmodules%2Flayout_builder%2Fsrc%2FEntity%2FLayoutBuilderSampleEntityGenerator.php;fp=web%2Fcore%2Fmodules%2Flayout_builder%2Fsrc%2FEntity%2FLayoutBuilderSampleEntityGenerator.php;h=9f606070e3fd8ea914811c0511e71deccf1f8450;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hp=0000000000000000000000000000000000000000;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0;p=yaffs-website diff --git a/web/core/modules/layout_builder/src/Entity/LayoutBuilderSampleEntityGenerator.php b/web/core/modules/layout_builder/src/Entity/LayoutBuilderSampleEntityGenerator.php new file mode 100644 index 000000000..9f606070e --- /dev/null +++ b/web/core/modules/layout_builder/src/Entity/LayoutBuilderSampleEntityGenerator.php @@ -0,0 +1,91 @@ +tempStoreFactory = $temp_store_factory; + $this->entityTypeManager = $entity_type_manager; + } + + /** + * Gets a sample entity for a given entity type and bundle. + * + * @param string $entity_type_id + * The entity type ID. + * @param string $bundle_id + * The bundle ID. + * + * @return \Drupal\Core\Entity\EntityInterface + * An entity. + */ + 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; + } + + /** + * Deletes a sample entity for a given entity type and bundle. + * + * @param string $entity_type_id + * The entity type ID. + * @param string $bundle_id + * The bundle ID. + * + * @return $this + */ + 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; + } + +}