diff -u b/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php b/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php --- b/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php +++ b/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php @@ -227,7 +227,7 @@ public function getContextsFromRoute($value, $definition, $name, array $defaults) { $contexts = []; - $id = $this->extractIdFromRoute($value, $defaults); + $id = $this->extractIdFromRoute($value, $definition, $name, $defaults); if ($id) { $section_list = $this->getSectionListFromId($id); $contexts['entity'] = EntityContext::fromEntity($section_list); @@ -238,7 +238,7 @@ /** * {@inheritdoc} */ - protected function extractIdFromRoute($value, array $defaults) { + protected function extractIdFromRoute($value, $definition, $name, array $defaults) { if (is_string($value) && strpos($value, '.') !== FALSE) { return $value; } diff -u b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php --- b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php +++ b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php @@ -108,7 +108,7 @@ public function getContextsFromRoute($value, $definition, $name, array $defaults) { $contexts = []; - $id = $this->extractIdFromRoute($value, $defaults); + $id = $this->extractIdFromRoute($value, $definition, $name, $defaults); if ($id) { $entity = $this->getSectionListFromId($id)->getEntity(); $contexts['entity'] = EntityContext::fromEntity($entity); @@ -120,7 +120,7 @@ /** * {@inheritdoc} */ - protected function extractIdFromRoute($value, array $defaults) { + protected function extractIdFromRoute($value, $definition, $name, array $defaults) { if (strpos($value, '.') !== FALSE) { return $value; } 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 @@ -102,7 +102,7 @@ foreach ($contexts as $name => $context) { $plugin->setContext($name, $context); } - return $plugin; + return $contexts ? $plugin : NULL; } } only in patch2: unchanged: --- a/core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php +++ b/core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php @@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; +use Drupal\Core\Plugin\Context\ContextInterface; use Drupal\layout_builder\Entity\LayoutBuilderSampleEntityGenerator; use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface; use Drupal\layout_builder\Plugin\SectionStorage\DefaultsSectionStorage; @@ -51,7 +52,7 @@ protected function setUp() { 'id' => 'defaults', 'class' => DefaultsSectionStorage::class, ]); - $this->plugin = new DefaultsSectionStorage([], '', $definition, $this->entityTypeManager->reveal(), $entity_type_bundle_info->reveal(), $sample_entity_generator->reveal()); + $this->plugin = new TestDefaultsSectionStorage([], '', $definition, $this->entityTypeManager->reveal(), $entity_type_bundle_info->reveal(), $sample_entity_generator->reveal()); } /** @@ -61,8 +62,12 @@ protected function setUp() { public function testThirdPartySettings() { // Set an initial value on the section list. $section_list = $this->prophesize(LayoutEntityDisplayInterface::class); + + $context = $this->prophesize(ContextInterface::class); + $context->getContextValue()->willReturn($section_list->reveal()); + $this->plugin->setContext('entity', $context->reveal()); + $section_list->getThirdPartySetting('the_module', 'the_key', NULL)->willReturn('value 1'); - $this->plugin->setSectionList($section_list->reveal()); // The plugin returns the initial value. $this->assertSame('value 1', $this->plugin->getThirdPartySetting('the_module', 'the_key')); @@ -424,3 +429,21 @@ public function testBuildRoutes() { } } + +class TestDefaultsSectionStorage extends DefaultsSectionStorage { + + /** + * {@inheritdoc} + */ + public function extractIdFromRoute($value, $definition, $name, array $defaults) { + return parent::extractIdFromRoute($value, $definition, $name, $defaults); + } + + /** + * {@inheritdoc} + */ + public function getSectionListFromId($id) { + return parent::getSectionListFromId($id); + } + +} only in patch2: unchanged: --- a/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php +++ b/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php @@ -56,7 +56,7 @@ protected function setUp() { 'id' => 'overrides', 'class' => OverridesSectionStorage::class, ]); - $this->plugin = new OverridesSectionStorage([], 'overrides', $definition, $this->entityTypeManager->reveal(), $this->entityFieldManager->reveal()); + $this->plugin = new TestOverridesSectionStorage([], 'overrides', $definition, $this->entityTypeManager->reveal(), $this->entityFieldManager->reveal()); } /** @@ -388,3 +388,21 @@ public function testBuildRoutes() { } } + +class TestOverridesSectionStorage extends OverridesSectionStorage { + + /** + * {@inheritdoc} + */ + public function extractIdFromRoute($value, $definition, $name, array $defaults) { + return parent::extractIdFromRoute($value, $definition, $name, $defaults); + } + + /** + * {@inheritdoc} + */ + public function getSectionListFromId($id) { + return parent::getSectionListFromId($id); + } + +} only in patch2: unchanged: --- a/core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php +++ b/core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\layout_builder\Unit; +use Drupal\Component\Plugin\Context\ContextInterface; use Drupal\Component\Plugin\Factory\FactoryInterface; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; @@ -58,30 +59,15 @@ public function testLoadEmpty() { $this->assertInstanceOf(SectionStorageInterface::class, $result); } - /** - * @covers ::loadFromStorageId - */ - public function testLoadFromStorageId() { - $section_list = $this->prophesize(SectionListInterface::class); - $this->plugin->setSectionList($section_list->reveal())->will(function () { - return $this; - }); - $this->plugin->getSectionListFromId('the_storage_id')->willReturn($section_list->reveal()); - - $result = $this->manager->loadFromStorageId('the_plugin_id', 'the_storage_id'); - $this->assertInstanceOf(SectionStorageInterface::class, $result); - } - /** * @covers ::loadFromRoute */ public function testLoadFromRoute() { - $section_list = $this->prophesize(SectionListInterface::class); - $this->plugin->extractIdFromRoute('the_value', [], 'the_parameter_name', [])->willReturn('the_storage_id'); - $this->plugin->getSectionListFromId('the_storage_id')->willReturn($section_list->reveal()); - $this->plugin->setSectionList($section_list->reveal())->will(function () { - return $this; - }); + $contexts = [ + 'the_context' => $this->prophesize(ContextInterface::class)->reveal(), + ]; + $this->plugin->getContextsFromRoute('the_value', [], 'the_parameter_name', [])->willReturn($contexts); + $this->plugin->setContext('the_context', $contexts['the_context'])->shouldBeCalled(); $result = $this->manager->loadFromRoute('the_plugin_id', 'the_value', [], 'the_parameter_name', []); $this->assertInstanceOf(SectionStorageInterface::class, $result); @@ -91,7 +77,7 @@ public function testLoadFromRoute() { * @covers ::loadFromRoute */ public function testLoadFromRouteNull() { - $this->plugin->extractIdFromRoute('the_value', [], 'the_parameter_name', ['_route' => 'the_route_name'])->willReturn(NULL); + $this->plugin->getContextsFromRoute('the_value', [], 'the_parameter_name', ['_route' => 'the_route_name'])->willReturn([]); $result = $this->manager->loadFromRoute('the_plugin_id', 'the_value', [], 'the_parameter_name', ['_route' => 'the_route_name']); $this->assertNull($result);