3 namespace Drupal\layout_builder;
5 use Drupal\Core\TempStore\SharedTempStoreFactory;
8 * Provides a mechanism for loading layouts from tempstore.
12 class LayoutTempstoreRepository implements LayoutTempstoreRepositoryInterface {
15 * The shared tempstore factory.
17 * @var \Drupal\Core\TempStore\SharedTempStoreFactory
19 protected $tempStoreFactory;
22 * LayoutTempstoreRepository constructor.
24 * @param \Drupal\Core\TempStore\SharedTempStoreFactory $temp_store_factory
25 * The shared tempstore factory.
27 public function __construct(SharedTempStoreFactory $temp_store_factory) {
28 $this->tempStoreFactory = $temp_store_factory;
34 public function get(SectionStorageInterface $section_storage) {
35 $id = $section_storage->getStorageId();
36 $tempstore = $this->getTempstore($section_storage)->get($id);
37 if (!empty($tempstore['section_storage'])) {
38 $storage_type = $section_storage->getStorageType();
39 $section_storage = $tempstore['section_storage'];
41 if (!($section_storage instanceof SectionStorageInterface)) {
42 throw new \UnexpectedValueException(sprintf('The entry with storage type "%s" and ID "%s" is invalid', $storage_type, $id));
45 return $section_storage;
51 public function set(SectionStorageInterface $section_storage) {
52 $id = $section_storage->getStorageId();
53 $this->getTempstore($section_storage)->set($id, ['section_storage' => $section_storage]);
59 public function delete(SectionStorageInterface $section_storage) {
60 $id = $section_storage->getStorageId();
61 $this->getTempstore($section_storage)->delete($id);
65 * Gets the shared tempstore.
67 * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
68 * The section storage.
70 * @return \Drupal\Core\TempStore\SharedTempStore
73 protected function getTempstore(SectionStorageInterface $section_storage) {
74 $collection = 'layout_builder.section_storage.' . $section_storage->getStorageType();
75 return $this->tempStoreFactory->get($collection);