263b767f729871845c3d36c120752cf520e97e97
[yaffs-website] / web / core / modules / layout_builder / src / Routing / LayoutTempstoreParamConverter.php
1 <?php
2
3 namespace Drupal\layout_builder\Routing;
4
5 use Drupal\Core\ParamConverter\ParamConverterInterface;
6 use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
7 use Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface;
8 use Symfony\Component\Routing\Route;
9
10 /**
11  * Loads the section storage from the layout tempstore.
12  *
13  * @internal
14  */
15 class LayoutTempstoreParamConverter implements ParamConverterInterface {
16
17   /**
18    * The layout tempstore repository.
19    *
20    * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
21    */
22   protected $layoutTempstoreRepository;
23
24   /**
25    * The section storage manager.
26    *
27    * @var \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface
28    */
29   protected $sectionStorageManager;
30
31   /**
32    * Constructs a new LayoutTempstoreParamConverter.
33    *
34    * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
35    *   The layout tempstore repository.
36    * @param \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface $section_storage_manager
37    *   The section storage manager.
38    */
39   public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, SectionStorageManagerInterface $section_storage_manager) {
40     $this->layoutTempstoreRepository = $layout_tempstore_repository;
41     $this->sectionStorageManager = $section_storage_manager;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function convert($value, $definition, $name, array $defaults) {
48     if (isset($defaults['section_storage_type']) && $this->sectionStorageManager->hasDefinition($defaults['section_storage_type'])) {
49       if ($section_storage = $this->sectionStorageManager->loadFromRoute($defaults['section_storage_type'], $value, $definition, $name, $defaults)) {
50         // Pass the plugin through the tempstore repository.
51         return $this->layoutTempstoreRepository->get($section_storage);
52       }
53     }
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function applies($definition, $name, Route $route) {
60     return !empty($definition['layout_builder_tempstore']);
61   }
62
63 }