You are here

function layout_builder_styles_update_8001 in Layout Builder Styles 8

Migrate away from using section component TPS to the old additional property.

File

./layout_builder_styles.install, line 15
Layout Builder Styles install file.

Code

function layout_builder_styles_update_8001() {

  // Make sure that the patch that applies TPS to section components is present.
  // If it's not, this update cannot work, because we cannot retrieve the TPS
  // from the section components without that interface.
  if (!in_array('Drupal\\Core\\Config\\Entity\\ThirdPartySettingsInterface', class_implements('Drupal\\layout_builder\\Entity\\LayoutBuilderEntityViewDisplay'))) {
    throw new \Exception('Cannot run layout builder styles migration because the core patch that added third party settings to section components is not installed.');
  }

  // Function that moves the layout builder styles on section components from
  // being stored in Third Party Settings to instead be stored in the
  // "additional" property. The patch that added TPS support to section
  // components is not viable anymore, so it cannot be used long term.
  $migrateSectionComponents = function ($sections) {
    $count = 0;
    foreach ($sections as $sectionUuid => $section) {

      /** @var \Drupal\layout_builder\Section $section */
      $components = $section
        ->getComponents();
      foreach ($components as $componentUuid => $component) {

        /** @var \Drupal\layout_builder\SectionComponent $component */
        $style = $component
          ->getThirdPartySetting('layout_builder_styles', 'style');
        if ($style) {
          $component
            ->unsetThirdPartySetting('layout_builder_styles', 'style');
          $component
            ->set('layout_builder_styles_style', $style);
          $count++;
        }
      }
    }
    return $count;
  };

  // Find all entity displays that have layout builder enabled.
  $allDisplays = \Drupal::entityTypeManager()
    ->getStorage('entity_view_display')
    ->loadMultiple();
  foreach ($allDisplays as $display) {
    if (!$display instanceof LayoutBuilderEntityViewDisplay) {
      continue;
    }
    if ($display
      ->isLayoutBuilderEnabled()) {
      $updatedComponents = $migrateSectionComponents($display
        ->getSections());
      if ($updatedComponents > 0) {
        $display
          ->save();
        \Drupal::logger('layout_builder_styles')
          ->info('Migrated TPS on %count section component(s) for the default layout on entity view display %id', [
          '%count' => $updatedComponents,
          '%id' => $display
            ->id(),
        ]);
      }
    }

    // Check for overrides now.
    if ($display
      ->isOverridable()) {

      // Process all entities that are associated with the entity type that this
      // entity view display belongs to.
      $entityTypeId = $display
        ->getTargetEntityTypeId();
      $entityType = \Drupal::entityTypeManager()
        ->getDefinition($entityTypeId);
      $properties = [];
      if ($entityType
        ->hasKey('bundle')) {
        $properties[$entityType
          ->getKey('bundle')] = $display
          ->getTargetBundle();
      }
      $entities = \Drupal::entityTypeManager()
        ->getStorage($entityTypeId)
        ->loadByProperties($properties);
      foreach ($entities as $entity) {

        /** @var \Drupal\layout_builder\Field\LayoutSectionItemList $layoutFieldData */
        $layoutFieldData = $entity->{OverridesSectionStorage::FIELD_NAME};
        $updatedComponents = $migrateSectionComponents($layoutFieldData
          ->getSections());
        if ($updatedComponents > 0) {
          $entity
            ->save();
          \Drupal::logger('layout_builder_styles')
            ->info('Migrated TPS on %count section component(s) on overridden layout on entity id %entity_id for entity view display %display_id', [
            '%count' => $updatedComponents,
            '%display_id' => $display
              ->id(),
            '%entity_id' => $entity
              ->id(),
          ]);
        }
      }
    }
  }
}