Pull merge.
[yaffs-website] / web / core / modules / layout_builder / src / SectionStorage / SectionStorageManager.php
1 <?php
2
3 namespace Drupal\layout_builder\SectionStorage;
4
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Extension\ModuleHandlerInterface;
7 use Drupal\Core\Plugin\DefaultPluginManager;
8 use Drupal\layout_builder\Annotation\SectionStorage;
9 use Drupal\layout_builder\SectionStorageInterface;
10
11 /**
12  * Provides the Section Storage type plugin manager.
13  *
14  * @internal
15  *   Layout Builder is currently experimental and should only be leveraged by
16  *   experimental modules and development releases of contributed modules.
17  *   See https://www.drupal.org/core/experimental for more information.
18  */
19 class SectionStorageManager extends DefaultPluginManager implements SectionStorageManagerInterface {
20
21   /**
22    * Constructs a new SectionStorageManager object.
23    *
24    * @param \Traversable $namespaces
25    *   An object that implements \Traversable which contains the root paths
26    *   keyed by the corresponding namespace to look for plugin implementations.
27    * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
28    *   Cache backend instance to use.
29    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
30    *   The module handler to invoke the alter hook with.
31    */
32   public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
33     parent::__construct('Plugin/SectionStorage', $namespaces, $module_handler, SectionStorageInterface::class, SectionStorage::class);
34
35     $this->alterInfo('layout_builder_section_storage');
36     $this->setCacheBackend($cache_backend, 'layout_builder_section_storage_plugins');
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public function loadEmpty($id) {
43     return $this->createInstance($id);
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public function loadFromStorageId($type, $id) {
50     /** @var \Drupal\layout_builder\SectionStorageInterface $plugin */
51     $plugin = $this->createInstance($type);
52     return $plugin->setSectionList($plugin->getSectionListFromId($id));
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function loadFromRoute($type, $value, $definition, $name, array $defaults) {
59     /** @var \Drupal\layout_builder\SectionStorageInterface $plugin */
60     $plugin = $this->createInstance($type);
61     if ($id = $plugin->extractIdFromRoute($value, $definition, $name, $defaults)) {
62       try {
63         return $plugin->setSectionList($plugin->getSectionListFromId($id));
64       }
65       catch (\InvalidArgumentException $e) {
66         // Intentionally empty.
67       }
68     }
69   }
70
71 }