class LayoutTempstoreRepository

Same name and namespace in other branches
  1. 9 core/modules/layout_builder/src/LayoutTempstoreRepository.php \Drupal\layout_builder\LayoutTempstoreRepository
  2. 8.9.x core/modules/layout_builder/src/LayoutTempstoreRepository.php \Drupal\layout_builder\LayoutTempstoreRepository
  3. 11.x core/modules/layout_builder/src/LayoutTempstoreRepository.php \Drupal\layout_builder\LayoutTempstoreRepository

Provides a mechanism for loading layouts from tempstore.

Hierarchy

Expanded class hierarchy of LayoutTempstoreRepository

1 file declares its use of LayoutTempstoreRepository
LayoutTempstoreRepositoryTest.php in core/modules/layout_builder/tests/src/Unit/LayoutTempstoreRepositoryTest.php
1 string reference to 'LayoutTempstoreRepository'
layout_builder.services.yml in core/modules/layout_builder/layout_builder.services.yml
core/modules/layout_builder/layout_builder.services.yml
1 service uses LayoutTempstoreRepository
layout_builder.tempstore_repository in core/modules/layout_builder/layout_builder.services.yml
Drupal\layout_builder\LayoutTempstoreRepository

File

core/modules/layout_builder/src/LayoutTempstoreRepository.php, line 10

Namespace

Drupal\layout_builder
View source
class LayoutTempstoreRepository implements LayoutTempstoreRepositoryInterface {
  
  /**
   * The shared tempstore factory.
   *
   * @var \Drupal\Core\TempStore\SharedTempStoreFactory
   */
  protected $tempStoreFactory;
  
  /**
   * The static cache of loaded values.
   *
   * @var \Drupal\layout_builder\SectionStorageInterface[]
   */
  protected array $cache = [];
  
  /**
   * LayoutTempstoreRepository constructor.
   *
   * @param \Drupal\Core\TempStore\SharedTempStoreFactory $temp_store_factory
   *   The shared tempstore factory.
   */
  public function __construct(SharedTempStoreFactory $temp_store_factory) {
    $this->tempStoreFactory = $temp_store_factory;
  }
  
  /**
   * {@inheritdoc}
   */
  public function get(SectionStorageInterface $section_storage) {
    $key = $this->getKey($section_storage);
    // Check if the storage is present in the static cache.
    if (isset($this->cache[$key])) {
      return $this->cache[$key];
    }
    $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));
      }
      // Set the storage in the static cache.
      $this->cache[$key] = $section_storage;
    }
    return $section_storage;
  }
  
  /**
   * {@inheritdoc}
   */
  public function has(SectionStorageInterface $section_storage) {
    $key = $this->getKey($section_storage);
    // Check if the storage is present in the static cache.
    if (isset($this->cache[$key])) {
      return TRUE;
    }
    $tempstore = $this->getTempstore($section_storage)
      ->get($key);
    return !empty($tempstore['section_storage']);
  }
  
  /**
   * {@inheritdoc}
   */
  public function set(SectionStorageInterface $section_storage) {
    $key = $this->getKey($section_storage);
    $this->getTempstore($section_storage)
      ->set($key, [
      'section_storage' => $section_storage,
    ]);
    // Update the storage in the static cache.
    $this->cache[$key] = $section_storage;
  }
  
  /**
   * {@inheritdoc}
   */
  public function delete(SectionStorageInterface $section_storage) {
    $key = $this->getKey($section_storage);
    $this->getTempstore($section_storage)
      ->delete($key);
    // Remove the storage from the static cache.
    unset($this->cache[$key]);
  }
  
  /**
   * Gets the shared tempstore.
   *
   * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
   *   The section storage.
   *
   * @return \Drupal\Core\TempStore\SharedTempStore
   *   The tempstore.
   */
  protected function getTempstore(SectionStorageInterface $section_storage) {
    $collection = 'layout_builder.section_storage.' . $section_storage->getStorageType();
    return $this->tempStoreFactory
      ->get($collection);
  }
  
  /**
   * Gets the string to use as the tempstore key.
   *
   * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
   *   The section storage.
   *
   * @return string
   *   A unique string representing the section storage. This should include as
   *   much identifying information as possible about this particular storage,
   *   including information like the current language.
   */
  protected function getKey(SectionStorageInterface $section_storage) {
    if ($section_storage instanceof TempStoreIdentifierInterface) {
      return $section_storage->getTempstoreKey();
    }
    return $section_storage->getStorageId();
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
LayoutTempstoreRepository::$cache protected property The static cache of loaded values.
LayoutTempstoreRepository::$tempStoreFactory protected property The shared tempstore factory.
LayoutTempstoreRepository::delete public function Removes the tempstore version of a section storage. Overrides LayoutTempstoreRepositoryInterface::delete
LayoutTempstoreRepository::get public function Gets the tempstore version of a section storage, if it exists. Overrides LayoutTempstoreRepositoryInterface::get
LayoutTempstoreRepository::getKey protected function Gets the string to use as the tempstore key.
LayoutTempstoreRepository::getTempstore protected function Gets the shared tempstore.
LayoutTempstoreRepository::has public function Checks for the existence of a tempstore version of a section storage. Overrides LayoutTempstoreRepositoryInterface::has
LayoutTempstoreRepository::set public function Stores this section storage in tempstore. Overrides LayoutTempstoreRepositoryInterface::set
LayoutTempstoreRepository::__construct public function LayoutTempstoreRepository constructor.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.