You are here

class LayoutParagraphsLayoutTempstoreRepository in Layout Paragraphs 2.0.x

Layout Paragraphs Layout Tempstore Repository class definition.

Hierarchy

Expanded class hierarchy of LayoutParagraphsLayoutTempstoreRepository

7 files declare their use of LayoutParagraphsLayoutTempstoreRepository
ComponentFormBase.php in src/Form/ComponentFormBase.php
LayoutParagraphsBuilder.php in src/Element/LayoutParagraphsBuilder.php
LayoutParagraphsBuilderForm.php in src/Form/LayoutParagraphsBuilderForm.php
LayoutParagraphsBuilderFormatter.php in src/Plugin/Field/FieldFormatter/LayoutParagraphsBuilderFormatter.php
LayoutParagraphsTempstoreParamConverter.php in src/Routing/LayoutParagraphsTempstoreParamConverter.php

... See full list

1 string reference to 'LayoutParagraphsLayoutTempstoreRepository'
layout_paragraphs.services.yml in ./layout_paragraphs.services.yml
layout_paragraphs.services.yml
1 service uses LayoutParagraphsLayoutTempstoreRepository
layout_paragraphs.tempstore_repository in ./layout_paragraphs.services.yml
Drupal\layout_paragraphs\LayoutParagraphsLayoutTempstoreRepository

File

src/LayoutParagraphsLayoutTempstoreRepository.php, line 10

Namespace

Drupal\layout_paragraphs
View source
class LayoutParagraphsLayoutTempstoreRepository {

  /**
   * The shared tempstore factory.
   *
   * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
   */
  protected $tempStoreFactory;

  /**
   * LayoutTempstoreRepository constructor.
   *
   * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
   *   The shared tempstore factory.
   */
  public function __construct(PrivateTempStoreFactory $temp_store_factory) {
    $this->tempStoreFactory = $temp_store_factory;
  }

  /**
   * Get a layout paragraphs layout from the tempstore.
   */
  public function get(LayoutParagraphsLayout $layout_paragraphs_layout) {
    $key = $this
      ->getStorageKey($layout_paragraphs_layout);
    $tempstore_layout = $this
      ->getWithStorageKey($key);

    // Editor isn't in tempstore yet, so add it.
    if (empty($tempstore_layout)) {
      $tempstore_layout = $this
        ->set($layout_paragraphs_layout);
    }
    return $tempstore_layout;
  }

  /**
   * Get a layout paragraphs layout frome the tempstore using its storage key.
   *
   * @param string $key
   *   The storage key.
   *
   * @return \Drupal\layout_paragraphs\LayoutParagraphsLayout
   *   The layout.
   */
  public function getWithStorageKey(string $key) {
    return $this->tempStoreFactory
      ->get('layout_paragraphs')
      ->get($key);
  }

  /**
   * Save a layout paragraphs layout to the tempstore.
   */
  public function set(LayoutParagraphsLayout $layout_paragraphs_layout) {
    $key = $this
      ->getStorageKey($layout_paragraphs_layout);
    $this->tempStoreFactory
      ->get('layout_paragraphs')
      ->set($key, $layout_paragraphs_layout);
    return $layout_paragraphs_layout;
  }

  /**
   * Delete a layout from tempstore.
   */
  public function delete(LayoutParagraphsLayout $layout_paragraphs_layout) {
    $key = $this
      ->getStorageKey($layout_paragraphs_layout);
    $this->tempStoreFactory
      ->get('layout_paragraphs')
      ->delete($key);
  }

  /**
   * Returns a unique key for storing the layout.
   *
   * @param \Drupal\layout_paragraphs\LayoutParagraphsLayout $layout_paragraphs_layout
   *   The layout object.
   *
   * @return string
   *   The unique key.
   */
  public function getStorageKey(LayoutParagraphsLayout $layout_paragraphs_layout) {
    return $layout_paragraphs_layout
      ->id();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LayoutParagraphsLayoutTempstoreRepository::$tempStoreFactory protected property The shared tempstore factory.
LayoutParagraphsLayoutTempstoreRepository::delete public function Delete a layout from tempstore.
LayoutParagraphsLayoutTempstoreRepository::get public function Get a layout paragraphs layout from the tempstore.
LayoutParagraphsLayoutTempstoreRepository::getStorageKey public function Returns a unique key for storing the layout.
LayoutParagraphsLayoutTempstoreRepository::getWithStorageKey public function Get a layout paragraphs layout frome the tempstore using its storage key.
LayoutParagraphsLayoutTempstoreRepository::set public function Save a layout paragraphs layout to the tempstore.
LayoutParagraphsLayoutTempstoreRepository::__construct public function LayoutTempstoreRepository constructor.