diff --git a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php index bae8174211..c3a50ebc23 100644 --- a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php +++ b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php @@ -247,14 +247,12 @@ public function buildMultiple(array $entities) { $build_list[$id]['_layout_builder'] = []; $cacheability = new CacheableMetadata(); - $contexts = $this->getContextsForEntity($entity); - $storage = $this->sectionStorageManager()->findByContext($contexts, $cacheability); + $contexts = []; + $sections = $this->getRuntimeSections($entity, $cacheability, $contexts); // The render array is built based on decisions made by @SectionStorage // plugins and therefore it needs to depend on the accumulated // cacheability of those decisions. $cacheability->applyTo($build_list[$id]['_layout_builder']); - - $sections = $storage ? $storage->getSections() : []; if ($sections) { foreach ($build_list[$id] as $name => $build_part) { $field_definition = $this->getFieldDefinition($name); @@ -303,17 +301,23 @@ protected function getContextsForEntity(FieldableEntityInterface $entity) { * @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface|null $cacheability * (optional) Refinable cacheability object, which will be populated based * on the cacheability of each section storage candidate. + * @param \Drupal\Component\Plugin\Context\ContextInterface[]|null $contexts + * (optional) The contexts which should be used to determine which storage + * to return. * * @return \Drupal\layout_builder\Section[] * The sections. * * @todo Deprecate this method in https://www.drupal.org/node/2986403. */ - protected function getRuntimeSections(FieldableEntityInterface $entity, RefinableCacheableDependencyInterface &$cacheability = NULL) { + protected function getRuntimeSections(FieldableEntityInterface $entity, RefinableCacheableDependencyInterface &$cacheability = NULL, &$contexts = NULL) { if (!$cacheability) { $cacheability = new CacheableMetadata(); } - $storage = $this->sectionStorageManager()->findByContext($this->getContextsForEntity($entity), $cacheability); + if (!$contexts) { + $contexts = $this->getContextsForEntity($entity); + } + $storage = $this->sectionStorageManager()->findByContext($contexts, $cacheability); return $storage ? $storage->getSections() : []; } diff --git a/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php b/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php index 240ddd951c..82e7dafbe8 100644 --- a/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php +++ b/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php @@ -31,17 +31,14 @@ public function load($type, array $contexts = []); /** * Finds the section storage to load based on available contexts. * - * After calling this method the $cacheability parameter will reflect the - * cacheability information used to determine the correct section storage. - * This must be applied to any output that uses the result of this method. - * * @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts * The contexts which should be used to determine which storage to return. * @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface $cacheability * Refinable cacheability object, which will be populated based on the - * cacheability of each section storage candidate. This is typically created - * directly before this method call and must be applied to a render array - * after this method call. + * cacheability of each section storage candidate. After calling this method + * this parameter will reflect the cacheability information used to + * determine the correct section storage. This must be associated with any + * output that uses the result of this method. * * @return \Drupal\layout_builder\SectionStorageInterface|null * The section storage if one matched all contexts, or NULL otherwise. diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderSectionStorageTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderSectionStorageTest.php index 3d0d00d131..f701c76926 100644 --- a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderSectionStorageTest.php +++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderSectionStorageTest.php @@ -30,7 +30,6 @@ protected function setUp() { // https://www.drupal.org/project/drupal/issues/2917777. $this->drupalPlaceBlock('local_tasks_block'); - // Create two nodes. $this->createContentType(['type' => 'bundle_with_section_field']); $this->createNode([ 'type' => 'bundle_with_section_field',