LayoutTempstoreRepository.php in Drupal 9
File
core/modules/layout_builder/src/LayoutTempstoreRepository.php
View source
<?php
namespace Drupal\layout_builder;
use Drupal\Core\TempStore\SharedTempStoreFactory;
class LayoutTempstoreRepository implements LayoutTempstoreRepositoryInterface {
protected $tempStoreFactory;
public function __construct(SharedTempStoreFactory $temp_store_factory) {
$this->tempStoreFactory = $temp_store_factory;
}
public function get(SectionStorageInterface $section_storage) {
$key = $this
->getKey($section_storage);
$tempstore = $this
->getTempstore($section_storage)
->get($key);
if (!empty($tempstore['section_storage'])) {
$storage_type = $section_storage
->getStorageType();
$section_storage = $tempstore['section_storage'];
if (!$section_storage instanceof SectionStorageInterface) {
throw new \UnexpectedValueException(sprintf('The entry with storage type "%s" and ID "%s" is invalid', $storage_type, $key));
}
}
return $section_storage;
}
public function has(SectionStorageInterface $section_storage) {
$key = $this
->getKey($section_storage);
$tempstore = $this
->getTempstore($section_storage)
->get($key);
return !empty($tempstore['section_storage']);
}
public function set(SectionStorageInterface $section_storage) {
$key = $this
->getKey($section_storage);
$this
->getTempstore($section_storage)
->set($key, [
'section_storage' => $section_storage,
]);
}
public function delete(SectionStorageInterface $section_storage) {
$key = $this
->getKey($section_storage);
$this
->getTempstore($section_storage)
->delete($key);
}
protected function getTempstore(SectionStorageInterface $section_storage) {
$collection = 'layout_builder.section_storage.' . $section_storage
->getStorageType();
return $this->tempStoreFactory
->get($collection);
}
protected function getKey(SectionStorageInterface $section_storage) {
if ($section_storage instanceof TempStoreIdentifierInterface) {
return $section_storage
->getTempstoreKey();
}
return $section_storage
->getStorageId();
}
}