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 has(SectionStorageInterface $section_storage) {
52 $id = $section_storage->getStorageId();
53 $tempstore = $this->getTempstore($section_storage)->get($id);
54 return !empty($tempstore['section_storage']);
60 public function set(SectionStorageInterface $section_storage) {
61 $id = $section_storage->getStorageId();
62 $this->getTempstore($section_storage)->set($id, ['section_storage' => $section_storage]);
68 public function delete(SectionStorageInterface $section_storage) {
69 $id = $section_storage->getStorageId();
70 $this->getTempstore($section_storage)->delete($id);
74 * Gets the shared tempstore.
76 * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
77 * The section storage.
79 * @return \Drupal\Core\TempStore\SharedTempStore
82 protected function getTempstore(SectionStorageInterface $section_storage) {
83 $collection = 'layout_builder.section_storage.' . $section_storage->getStorageType();
84 return $this->tempStoreFactory->get($collection);