diff --git a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php index bdde3583af..5558f689be 100644 --- a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php +++ b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php @@ -281,7 +281,7 @@ protected function getRuntimeSections(FieldableEntityInterface $entity) { if ($this->isOverridable()) { /** @var \Drupal\layout_builder\SectionStorageInterface $storage */ $storage = \Drupal::service('plugin.manager.layout_builder.section_storage') - ->loadFromContext([ + ->findByContext([ 'view_mode' => new Context(ContextDefinition::create('string'), $this->getMode()), 'entity' => EntityContext::fromEntity($entity), ]); diff --git a/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php b/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php index 0e18d4a86e..819f1c4781 100644 --- a/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php +++ b/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php @@ -59,23 +59,8 @@ protected function findDefinitions() { /** * {@inheritdoc} */ - public function loadEmpty($id) { - return $this->createInstance($id); - } - - /** - * {@inheritdoc} - */ - public function loadFromContext(array $contexts) { - $storage_types = array_keys($this->getDefinitionsForContexts($contexts)); - - foreach ($storage_types as $type) { - $plugin = $this->loadWithContextsApplied($type, $contexts); - if ($plugin && $plugin->access('load')) { - return $plugin; - } - } - return NULL; + public function loadEmpty($type) { + return $this->createInstance($type); } /** @@ -83,21 +68,13 @@ public function loadFromContext(array $contexts) { */ public function loadFromRoute($type, $value, $definition, $name, array $defaults) { $contexts = $this->loadEmpty($type)->getContextsFromRoute($value, $definition, $name, $defaults); - return $this->loadWithContextsApplied($type, $contexts); + return $this->loadFromContext($type, $contexts); } /** - * Loads a section storage with the provided contexts applied. - * - * @param string $type - * The section storage type. - * @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts - * The contexts available for this storage to use. - * - * @return \Drupal\layout_builder\SectionStorageInterface|null - * The section storage. + * {@inheritdoc} */ - protected function loadWithContextsApplied($type, array $contexts) { + public function loadFromContext($type, array $contexts) { $plugin = $this->loadEmpty($type); try { $this->contextHandler()->applyContextMapping($plugin, $contexts); @@ -108,4 +85,19 @@ protected function loadWithContextsApplied($type, array $contexts) { return $plugin; } + /** + * {@inheritdoc} + */ + public function findByContext(array $contexts) { + $storage_types = array_keys($this->getDefinitionsForContexts($contexts)); + + foreach ($storage_types as $type) { + $plugin = $this->loadFromContext($type, $contexts); + if ($plugin && $plugin->access('load')) { + return $plugin; + } + } + return NULL; + } + } diff --git a/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php b/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php index b7627e9a4a..4e62e18fb0 100644 --- a/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php +++ b/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php @@ -17,24 +17,13 @@ interface SectionStorageManagerInterface extends ContextAwarePluginManagerInterf /** * Loads a section storage with no associated section list. * - * @param string $id - * The ID of the section storage being instantiated. + * @param string $type + * The type of section storage being instantiated. * * @return \Drupal\layout_builder\SectionStorageInterface * The section storage. */ - public function loadEmpty($id); - - /** - * 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); + public function loadEmpty($type); /** * Loads a section storage populated with a section list derived from a route. @@ -57,4 +46,28 @@ public function loadFromContext(array $contexts); */ public function loadFromRoute($type, $value, $definition, $name, array $defaults); + /** + * Loads a section storage with the provided contexts applied. + * + * @param string $type + * The section storage type. + * @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts + * The contexts available for this storage to use. + * + * @return \Drupal\layout_builder\SectionStorageInterface|null + * The section storage. + */ + public function loadFromContext($type, array $contexts); + + /** + * Finds the section storage to load based on available contexts. + * + * @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 findByContext(array $contexts); + } diff --git a/core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php b/core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php index dce906aa6d..239cea7288 100644 --- a/core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php +++ b/core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php @@ -93,24 +93,44 @@ public function testLoadFromRoute() { 'the_context' => $this->prophesize(ContextInterface::class)->reveal(), ]; $plugin->getContextsFromRoute('the_value', [], 'the_parameter_name', [])->willReturn($contexts); - $plugin->access('load')->willReturn(TRUE); + + $this->contextHandler->applyContextMapping($plugin, $contexts)->shouldBeCalled(); $result = $this->manager->loadFromRoute('the_plugin_id', 'the_value', [], 'the_parameter_name', []); $this->assertSame($plugin->reveal(), $result); } /** - * @covers ::loadFromRoute + * @covers ::loadFromContext */ - public function testLoadFromRouteNull() { + public function testLoadFromContext() { $plugin = $this->prophesize(SectionStorageInterface::class); $this->factory->createInstance('the_plugin_id', [])->willReturn($plugin->reveal()); - $plugin->getContextsFromRoute('the_value', [], 'the_parameter_name', ['_route' => 'the_route_name'])->willReturn([]); - $this->contextHandler->applyContextMapping($plugin, [])->willThrow(new ContextException()); - $plugin->access('load')->shouldNotBeCalled(); + $contexts = [ + 'the_context' => $this->prophesize(ContextInterface::class)->reveal(), + ]; - $result = $this->manager->loadFromRoute('the_plugin_id', 'the_value', [], 'the_parameter_name', ['_route' => 'the_route_name']); + $this->contextHandler->applyContextMapping($plugin, $contexts)->shouldBeCalled(); + + $result = $this->manager->loadFromContext('the_plugin_id', $contexts); + $this->assertSame($plugin->reveal(), $result); + } + + /** + * @covers ::loadFromContext + */ + public function testLoadFromContextNull() { + $plugin = $this->prophesize(SectionStorageInterface::class); + $this->factory->createInstance('the_plugin_id', [])->willReturn($plugin->reveal()); + + $contexts = [ + 'the_context' => $this->prophesize(ContextInterface::class)->reveal(), + ]; + + $this->contextHandler->applyContextMapping($plugin, $contexts)->willThrow(new ContextException()); + + $result = $this->manager->loadFromContext('the_plugin_id', $contexts); $this->assertNull($result); } @@ -136,11 +156,11 @@ public function testFindDefinitions() { } /** - * @covers ::loadFromContext + * @covers ::findByContext * - * @dataProvider providerTestLoadFromContext + * @dataProvider providerTestFindByContext */ - public function testLoadFromContext($access) { + public function testFindByContext($access) { $contexts = [ 'foo' => new Context(new ContextDefinition('foo')), ]; @@ -169,7 +189,7 @@ public function testLoadFromContext($access) { $this->factory->createInstance('missing_contexts', [])->willReturn($missing_contexts->reveal()); $this->factory->createInstance('provider_access', [])->willReturn($provider_access->reveal()); - $result = $this->manager->loadFromContext($contexts); + $result = $this->manager->findByContext($contexts); if ($access) { $this->assertSame($provider_access->reveal(), $result); } @@ -179,9 +199,9 @@ public function testLoadFromContext($access) { } /** - * Provides test data for ::testLoadFromContext(). + * Provides test data for ::testFindByContext(). */ - public function providerTestLoadFromContext() { + public function providerTestFindByContext() { $data = []; $data['true'] = [TRUE]; $data['false'] = [FALSE];