You are here

public function MigrateManager::recreateModerationStatesOnEntities in Workbench Moderation to Content Moderation 8

Create the moderation states on all entities based on WBM data.

File

src/MigrateManager.php, line 407

Class

MigrateManager
Manages migrating from WBM to CM.

Namespace

Drupal\wbm2cm

Code

public function recreateModerationStatesOnEntities() {
  $enabled_bundles = $this->migrateStore
    ->get('enabled_bundles');
  foreach ($enabled_bundles as $entity_type_id => $bundles) {
    $entity_storage = $this->entityTypeManager
      ->getStorage($entity_type_id);
    foreach ($bundles as $bundle_id) {

      // Get all entity revisions.
      $this->logger
        ->debug('Querying for all %bundle_id revisions...', [
        '%bundle_id' => $bundle_id,
      ]);
      $entity_revisions = \Drupal::entityQuery($entity_type_id)
        ->condition('type', $bundle_id)
        ->allRevisions()
        ->execute();
      $this->logger
        ->debug('Setting Content Moderation states on %bundle_id entities', [
        '%bundle_id' => $bundle_id,
      ]);

      // Update the state for all revisions if a state id is found.
      foreach ($entity_revisions as $revision_id => $id) {

        // Get the state if it exists.
        $state_map_key = "state_map.{$entity_type_id}.{$bundle_id}.{$revision_id}";
        $state_id = $this->stateMapStore
          ->get($state_map_key);
        if (!$state_id) {
          $this->logger
            ->debug('Skipping updating state on id:%id, revision:%revision_id because no state exists', [
            '%id' => $entity
              ->id(),
            '%revision_id' => $revision_id,
            '%state' => $state_id,
          ]);
          continue;
        }

        // Set the state.
        $entity = $entity_storage
          ->loadRevision($revision_id);
        $entity->moderation_state = $state_id;
        $entity
          ->save();
        $this->logger
          ->debug('Setting Workbench Moderation state field on id:%id, revision:%revision_id to %state', [
          '%id' => $entity
            ->id(),
          '%revision_id' => $revision_id,
          '%state' => $state_id,
        ]);

        // Remove the state from key value store to indicate the entity has
        // been successfully updated.
        $this->stateMapStore
          ->delete($state_map_key);

        // Handle translations.
        // Get all languages except the default (which we've already handled).
        $languages = $entity
          ->getTranslationLanguages(FALSE);
        $language_ids = [];
        foreach ($languages as $language) {
          $language_ids[] = $language
            ->getId();
        }
        foreach ($language_ids as $language_id) {

          // Get the state if it exists.
          // @todo how to get all revisions for this translation?
          $translated_entity = $entity
            ->getTranslation($language_id);
          $state_map_key = "state_map.{$entity_type_id}.{$bundle_id}.{$revision_id}.{$language_id}";
          $state_id = $this->stateMapStore
            ->get($state_map_key);
          if (!$state_id) {
            $this->logger
              ->debug('Skipping updating state on id:%id, revision:%revision_id, lang:%lang because no state exists', [
              '%id' => $translated_entity
                ->id(),
              '%revision_id' => $revision_id,
              '%lang' => $language_id,
              '%state' => $state_id,
            ]);
            continue;
          }

          // Set the state.
          $translated_entity->moderation_state = $state_id;
          $translated_entity
            ->save();
          $this->logger
            ->debug('Setting Workbench Moderation state field on id:%id, revision:%revision_id to %state', [
            '%id' => $translated_entity
              ->id(),
            '%revision_id' => $revision_id,
            '%state' => $state_id,
          ]);

          // Remove the state from key value store to indicate the entity has
          // been successfully updated.
          $this->stateMapStore
            ->delete($state_map_key);
        }
      }
    }
  }
}