diff -u b/core/modules/layout_builder/src/Annotation/SectionStorage.php b/core/modules/layout_builder/src/Annotation/SectionStorage.php --- b/core/modules/layout_builder/src/Annotation/SectionStorage.php +++ b/core/modules/layout_builder/src/Annotation/SectionStorage.php @@ -25,6 +25,10 @@ /** * The plugin weight. * + * When an entity with layout is rendered, section storage plugins are + * checked, in order of their weight, to determine which one should be used + * to render the layout. + * * @var int */ public $weight = 0; @@ -32,6 +36,12 @@ /** * Any required context definitions. * + * When an entity with layout is rendered, all section storage plugins which + * match a particular set of contexts are checked, in order of their weight, + * to determine which plugin should be used to render the layout. + * + * @see \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::getRuntimeSections() + * * @var array */ public $context = []; 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 @@ -244,6 +244,7 @@ if ($this->isOverridable()) { $contexts = [ 'entity' => new Context(ContextDefinition::create('entity'), $entity), + 'view_mode' => new Context(ContextDefinition::create('string'), $this->getMode()), ]; /** @var \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface $storage_manager */ @@ -254,12 +255,14 @@ $storage_types = array_keys($storage_manager->getDefinitionsForContexts($contexts)); // Loop through every section storage plugin until we find one that - // derives a valid section list from the context values. + // derives a section list from the context values. while (empty($section_list) && $storage_types) { $storage_type = array_shift($storage_types); $storage = $storage_manager->loadEmpty($storage_type); - $storage->setContext('entity', $contexts['entity']); + foreach ($contexts as $context_name => $context) { + $storage->setContext($context_name, $context); + } $section_list = $storage->getSectionListFromContext(); } } diff -u b/core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module b/core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module --- b/core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module +++ b/core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module @@ -69,2 +69,5 @@ $storages['overrides']->set('weight', -10); + + $storages['overrides_heavy'] = clone $storages['overrides']; + $storages['overrides_heavy']->set('weight', -8); } diff -u b/core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/SectionStorage/OverridesSectionStorage.php b/core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/SectionStorage/OverridesSectionStorage.php --- b/core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/SectionStorage/OverridesSectionStorage.php +++ b/core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/SectionStorage/OverridesSectionStorage.php @@ -10,7 +10,10 @@ * {@inheritdoc} */ public function getSectionListFromContext() { - \Drupal::state()->set('layout_builder_test_context_applied', TRUE); + \Drupal::state()->set('layout_builder_test_storage', [ + $this->getPluginDefinition()->get('weight'), + $this->getContextValue('view_mode'), + ]); return parent::getSectionListFromContext(); } diff -u b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php --- b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php +++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php @@ -531,7 +531,7 @@ * Tests that section loading is delegated to plugins during rendering. */ public function testRenderByContextualPluginDelegate() { - $state_key = 'layout_builder_test_context_applied'; + $state_key = 'layout_builder_test_storage'; /** @var \Drupal\Core\State\StateInterface $state */ $state = $this->container->get('state'); @@ -545,17 +545,13 @@ $this->getSession()->reload(); $state->resetCache(); - $this->assertTrue($state->get($state_key)); - - // Test that plugin weights are respected. - /** @var \Drupal\layout_builder\SectionStorage\SectionStorageDefinition[] $definitions */ - $definitions = $this->container - ->get('plugin.manager.layout_builder.section_storage') - ->getDefinitions(); - - $this->assertSame('overrides', key($definitions)); - $this->assertSame(-10, $definitions['overrides']->get('weight')); - $this->assertSame(0, $definitions['defaults']->get('weight')); + // During layout rendering, the storage plugin used for testing will set the + // state key to an array containing the plugin weight and view mode, which + // proves that the plugin matched the appropriate contexts and was actually + // used to render the layout. + list ($weight, $view_mode) = $state->get($state_key); + $this->assertSame(-10, $weight); + $this->assertSame('full', $view_mode); } }