You are here

function layout_builder_post_update_section_storage_context_mapping in Drupal 9

Update view displays that use Layout Builder to add empty context mappings.

File

core/modules/layout_builder/layout_builder.post_update.php, line 46
Post update functions for Layout Builder.

Code

function layout_builder_post_update_section_storage_context_mapping(&$sandbox = []) {
  $config_entity_updater = \Drupal::classResolver(ConfigEntityUpdater::class);
  $callback = function (EntityViewDisplayInterface $display) {
    $needs_update = FALSE;

    // Only update entity view displays where Layout Builder is enabled.
    if ($display instanceof LayoutEntityDisplayInterface && $display
      ->isLayoutBuilderEnabled()) {
      foreach ($display
        ->getSections() as $section) {

        // Add an empty context mapping to each section where one doesn't exist.
        $section
          ->setLayoutSettings($section
          ->getLayoutSettings() + [
          'context_mapping' => [],
        ]);

        // Flag this display as needing to be updated.
        $needs_update = TRUE;
      }
    }
    return $needs_update;
  };
  $config_entity_updater
    ->update($sandbox, 'entity_view_display', $callback);
}