\Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::getRuntimeSections() is now deprecated.
getRuntimeSections() was hardcoded to return either the overrides or defaults of an entity.
Instead, \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface::findByContext() should be used in conjunction with \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::getContextsForEntity().
While getRuntimeSections() has been updated to wrap the new API calls, it will not allow callers to adequately reflect the cacheability info built up during the selection of the correct section storage. The new API should be used directly and the cacheability info should be associated with the output:
Before
namespace Drupal\mymodule;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
class MyClass extends LayoutBuilderEntityViewDisplay {
public function buildMultiple(array $entities) {
$build_list = parent::buildMultiple($entities);
$my_service = \Drupal::service('my_module.service');
foreach ($entities as $id => $entity) {
$sections = $this->getRuntimeSections($entity);
foreach ($sections as $delta => $section) {
if (!$my_service->isSectionGood($section)) {
unset($build_list[$id]['_layout_builder'][$delta]);
}
}
}
return $build_list;
}
}
After
namespace Drupal\mymodule;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
class MyClass extends LayoutBuilderEntityViewDisplay {
public function buildMultiple(array $entities) {
$build_list = parent::buildMultiple($entities);
$my_service = \Drupal::service('my_module.service');
foreach ($entities as $id => $entity) {
$cacheability = CacheableMetadata::createFromRenderArray($build_list[$id]['_layout_builder']);
$storage = $this->sectionStorageManager()->findByContext($this->getContextsForEntity($entity), $cacheability);
if ($storage && $sections = $storage->getSections()) {
$cacheability->addCacheableDependency($my_service);
foreach ($sections as $delta => $section) {
if (!$my_service->isSectionGood($section)) {
unset($build_list[$id]['_layout_builder'][$delta]);
}
}
}
$cacheability->applyTo($build_list[$id]['_layout_builder']);
}
return $build_list;
}
}
Note the use of CacheableMetadata. Any decisions that may affect the rendered output should be reflected within the cacheability info that is associated with the render array. See https://www.drupal.org/docs/8/api/render-api/cacheability-of-render-arrays for more.
See the change record for findByContext() for more information.