diff --git a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php index 0b1fe4d91b..8ec05d66c6 100644 --- a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php +++ b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php @@ -245,12 +245,14 @@ public function buildMultiple(array $entities) { /** @var \Drupal\Core\Entity\EntityInterface $entity */ foreach ($entities as $id => $entity) { $cacheability = new CacheableMetadata(); - $sections = $this->getRuntimeSections($entity, $cacheability); + $contexts = $this->getContextsForEntity($entity); + $storage = $this->sectionStorageManager()->findByContext($contexts, $cacheability); - // Apply cacheability metadata to the build array. + // Apply cacheability information to the build array. $build_list[$id]['_layout_builder'] = []; $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); @@ -259,7 +261,6 @@ public function buildMultiple(array $entities) { } } - $contexts = $this->getContextsForEntity($entity); // @todo Remove in https://www.drupal.org/project/drupal/issues/3018782. $label = new TranslatableMarkup('@entity being viewed', [ '@entity' => $entity->getEntityType()->getSingularLabel(), @@ -297,12 +298,14 @@ protected function getContextsForEntity(FieldableEntityInterface $entity) { * * @param \Drupal\Core\Entity\FieldableEntityInterface $entity * The entity. - * @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface $cacheability - * (optional) Cacheability metadata object, which will be populated based on - * the cacheability of each section storage candidate. + * @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface|null $cacheability + * (optional) Refinable cacheability object, which will be populated based + * on the cacheability of each section storage candidate. * * @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) { if (!$cacheability) { diff --git a/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php b/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php index 32ea0edb8a..b18b17767d 100644 --- a/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php +++ b/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php @@ -4,6 +4,7 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Access\AccessResult; +use Drupal\Core\Cache\RefinableCacheableDependencyInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; @@ -451,4 +452,12 @@ public function access($operation, AccountInterface $account = NULL, $return_as_ return $return_as_object ? $result : $result->isAllowed(); } + /** + * {@inheritdoc} + */ + public function isApplicable(RefinableCacheableDependencyInterface $cacheability) { + // Defaults are always applicable. + return TRUE; + } + } diff --git a/core/modules/layout_builder/src/Plugin/SectionStorage/SectionStorageBase.php b/core/modules/layout_builder/src/Plugin/SectionStorage/SectionStorageBase.php index 4a9e8c623b..05a2de11e3 100644 --- a/core/modules/layout_builder/src/Plugin/SectionStorage/SectionStorageBase.php +++ b/core/modules/layout_builder/src/Plugin/SectionStorage/SectionStorageBase.php @@ -2,7 +2,6 @@ namespace Drupal\layout_builder\Plugin\SectionStorage; -use Drupal\Core\Cache\RefinableCacheableDependencyInterface; use Drupal\Core\Plugin\ContextAwarePluginBase; use Drupal\layout_builder\Routing\LayoutBuilderRoutesTrait; use Drupal\layout_builder\Section; @@ -109,11 +108,4 @@ public function getContextsDuringPreview() { return $this->getContexts(); } - /** - * {@inheritdoc} - */ - public function isApplicable(RefinableCacheableDependencyInterface $cacheability) { - return TRUE; - } - } diff --git a/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php b/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php index 8e17025052..fd14bac830 100644 --- a/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php +++ b/core/modules/layout_builder/src/SectionStorage/SectionStorageManager.php @@ -96,6 +96,10 @@ public function load($type, array $contexts = []) { public function findByContext(array $contexts, RefinableCacheableDependencyInterface $cacheability) { $storage_types = array_keys($this->contextHandler->filterPluginDefinitionsByContexts($contexts, $this->getDefinitions())); + // Add the manager as a cacheable dependency in order to vary by changes to + // the plugin definitions. + $cacheability->addCacheableDependency($this); + foreach ($storage_types as $type) { $plugin = $this->load($type, $contexts); if ($plugin && $plugin->isApplicable($cacheability)) { diff --git a/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php b/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php index 6b2b1cce1d..240ddd951c 100644 --- a/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php +++ b/core/modules/layout_builder/src/SectionStorage/SectionStorageManagerInterface.php @@ -32,13 +32,13 @@ 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 metadata used to determine the correct section storage. This - * must be applied to any output that results from the calling of this method. + * 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 - * Cacheability metadata object, which will be populated based on the + * 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. @@ -46,7 +46,7 @@ public function load($type, array $contexts = []); * @return \Drupal\layout_builder\SectionStorageInterface|null * The section storage if one matched all contexts, or NULL otherwise. * - * @see \Drupal\Core\Cache\CacheableMetadata + * @see \Drupal\Core\Cache\RefinableCacheableDependencyInterface */ public function findByContext(array $contexts, RefinableCacheableDependencyInterface $cacheability); diff --git a/core/modules/layout_builder/src/SectionStorageInterface.php b/core/modules/layout_builder/src/SectionStorageInterface.php index a8d257eb25..f20a3fc6f4 100644 --- a/core/modules/layout_builder/src/SectionStorageInterface.php +++ b/core/modules/layout_builder/src/SectionStorageInterface.php @@ -166,21 +166,21 @@ public function label(); public function save(); /** - * Returns if the section storage should be selected during plugin mapping. + * Returns if this section storage should be selected during plugin mapping. * * @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface $cacheability - * Cacheability metadata object, typically provided by the section storage + * Refinable cacheability object, typically provided by the section storage * manager. When implementing this method, populate $cacheability with any - * information that affects whether the storage is applicable. + * information that affects whether this storage is applicable. * * @return bool - * TRUE if the section storage can be used, FALSE otherwise. + * TRUE if this section storage can be used, FALSE otherwise. * * @internal * This method is intended to be called by * \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface::findByContext(). * - * @see \Drupal\Core\Cache\CacheableMetadata + * @see \Drupal\Core\Cache\RefinableCacheableDependencyInterface */ public function isApplicable(RefinableCacheableDependencyInterface $cacheability);