diff -u b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php --- b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php +++ b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php @@ -269,27 +269,14 @@ $sections = NULL; if ($this->isOverridable()) { - $contexts = [ - 'entity' => new Context(ContextDefinition::create('entity'), $entity), - 'view_mode' => new Context(ContextDefinition::create('string'), $this->getMode()), - ]; + /** @var \Drupal\layout_builder\SectionStorageInterface $storage */ + $storage = \Drupal::service('plugin.manager.layout_builder.section_storage') + ->loadFromContext([ + 'entity' => new Context(ContextDefinition::create('entity'), $entity), + 'view_mode' => new Context(ContextDefinition::create('string'), $this->getMode()), + ]); - /** @var \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface $storage_manager */ - $storage_manager = \Drupal::service('plugin.manager.layout_builder.section_storage'); - - // Use the contexts as a filtering mechanism to find all applicable - // section storage plugin IDs. - $storage_types = array_keys($storage_manager->getDefinitionsForContexts($contexts)); - - // Loop through every section storage plugin until we find one that - // derives a section list from the context values. - while (empty($sections) && $storage_types) { - $storage_type = array_shift($storage_types); - - $storage = $storage_manager->loadEmpty($storage_type); - foreach ($contexts as $context_name => $context) { - $storage->setContext($context_name, $context); - } + if ($storage) { $sections = $storage->getSections(); } } diff -u b/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php b/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php --- b/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php +++ b/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php @@ -68,0 +69,20 @@ + * {@inheritdoc} + */ + public function loadFromContext(array $contexts) { + $storage_types = array_keys($this->getDefinitionsForContexts($contexts)); + + foreach ($storage_types as $type) { + /** @var \Drupal\layout_builder\SectionStorageInterface $plugin */ + $plugin = $this->createInstance($type); + + foreach ($contexts as $name => $context) { + $plugin->setContext($name, $context); + } + if (count($plugin) > 0) { + return $plugin; + } + } + return NULL; + } + + /** diff -u b/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php b/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php --- b/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php +++ b/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php @@ -27,4 +27,15 @@ /** + * Loads a section storage populated with a section list derived from context. + * + * @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts + * The contexts which should be used to determine which storage to load. + * + * @return \Drupal\layout_builder\SectionStorageInterface|null + * The section storage if one matched all contexts, or NULL otherwise. + */ + public function loadFromContext(array $contexts); + + /** * Loads a section storage populated with a section list derived from a route. *