diff --git a/core/modules/layout_builder/src/Annotation/SectionStorage.php b/core/modules/layout_builder/src/Annotation/SectionStorage.php index 73924be8ad..9a52cff5da 100644 --- a/core/modules/layout_builder/src/Annotation/SectionStorage.php +++ b/core/modules/layout_builder/src/Annotation/SectionStorage.php @@ -42,7 +42,7 @@ class SectionStorage extends Plugin { * * @var array * - * @see \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::getRuntimeSections() + * @see \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface::findByContext() */ public $context = []; diff --git a/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php b/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php index 24c230055c..06e5a2036e 100644 --- a/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php +++ b/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php @@ -244,7 +244,15 @@ public function getContextsFromRoute($value, $definition, $name, array $defaults } /** - * {@inheritdoc} + * Extracts an entity from the route values. + * + * @param mixed $value + * The raw value from the route. + * @param array $defaults + * The route defaults array. + * + * @return \Drupal\Core\Entity\EntityInterface|null + * The entity for the route, or NULL if none exist. */ protected function extractEntityFromRoute($value, array $defaults) { // If a bundle is not provided but a value corresponding to the bundle key diff --git a/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php index 4eec93230b..b1db3c5a36 100644 --- a/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php +++ b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php @@ -116,7 +116,15 @@ public function getContextsFromRoute($value, $definition, $name, array $defaults } /** - * {@inheritdoc} + * Extracts an entity from the route values. + * + * @param mixed $value + * The raw value from the route. + * @param array $defaults + * The route defaults array. + * + * @return \Drupal\Core\Entity\EntityInterface|null + * The entity for the route, or NULL if none exist. */ protected function extractEntityFromRoute($value, array $defaults) { if (strpos($value, '.') !== FALSE) { diff --git a/core/modules/layout_builder/src/Routing/LayoutTempstoreParamConverter.php b/core/modules/layout_builder/src/Routing/LayoutTempstoreParamConverter.php index 946b5f35cf..e5976ca0bd 100644 --- a/core/modules/layout_builder/src/Routing/LayoutTempstoreParamConverter.php +++ b/core/modules/layout_builder/src/Routing/LayoutTempstoreParamConverter.php @@ -45,12 +45,17 @@ public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore * {@inheritdoc} */ public function convert($value, $definition, $name, array $defaults) { - if (isset($defaults['section_storage_type']) && $this->sectionStorageManager->hasDefinition($defaults['section_storage_type'])) { - $contexts = $this->sectionStorageManager->loadEmpty($defaults['section_storage_type'])->getContextsFromRoute($value, $definition, $name, $defaults); - if ($section_storage = $this->sectionStorageManager->load($defaults['section_storage_type'], $contexts)) { - // Pass the plugin through the tempstore repository. - return $this->layoutTempstoreRepository->get($section_storage); - } + // If no section storage type is specified or if it is invalid, return. + if (!isset($defaults['section_storage_type']) || !$this->sectionStorageManager->hasDefinition($defaults['section_storage_type'])) { + return NULL; + } + + // Load an empty instance and derive the available contexts. + $contexts = $this->sectionStorageManager->loadEmpty($defaults['section_storage_type'])->getContextsFromRoute($value, $definition, $name, $defaults); + // Attempt to load a full instance based on the context. + if ($section_storage = $this->sectionStorageManager->load($defaults['section_storage_type'], $contexts)) { + // Pass the plugin through the tempstore repository. + return $this->layoutTempstoreRepository->get($section_storage); } } diff --git a/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php b/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php index 5d4e8fb6f1..eaa3082e21 100644 --- a/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php +++ b/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php @@ -68,7 +68,7 @@ protected function findDefinitions() { /** * {@inheritdoc} */ - public function load($type, array $contexts) { + public function load($type, array $contexts = []) { $plugin = $this->loadEmpty($type); try { $this->contextHandler->applyContextMapping($plugin, $contexts); diff --git a/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php b/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php index 26d0f9b42b..63d8b4ad5e 100644 --- a/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php +++ b/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php @@ -7,6 +7,11 @@ /** * Provides the interface for a plugin manager of section storage types. * + * Note that this interface purposefully does not implement + * \Drupal\Component\Plugin\PluginManagerInterface, as the below methods exist + * to serve the use case of \Drupal\Component\Plugin\Factory\FactoryInterface + * and \Drupal\Component\Plugin\Mapper\MapperInterface. + * * @internal * Layout Builder is currently experimental and should only be leveraged by * experimental modules and development releases of contributed modules. @@ -20,12 +25,12 @@ interface SectionStorageManagerInterface extends DiscoveryInterface { * @param string $type * The section storage type. * @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts - * The contexts available for this storage to use. + * (optional) The contexts available for this storage to use. * * @return \Drupal\layout_builder\SectionStorageInterface|null - * The section storage. + * The section storage or NULL if its context requirements are not met. */ - public function load($type, array $contexts); + public function load($type, array $contexts = []); /** * Finds the section storage to load based on available contexts.