You are here

public function MultiversionManager::enableEntityTypes in Multiversion 8

Same name and namespace in other branches
  1. 8.2 src/MultiversionManager.php \Drupal\multiversion\MultiversionManager::enableEntityTypes()

@todo Ensure nothing breaks if the migration is run twice.

Overrides MultiversionManagerInterface::enableEntityTypes

File

src/MultiversionManager.php, line 265

Class

MultiversionManager

Namespace

Drupal\multiversion

Code

public function enableEntityTypes($entity_types_to_enable = NULL) {
  $entity_types = $entity_types_to_enable !== NULL ? $entity_types_to_enable : $this
    ->getSupportedEntityTypes();
  $enabled_entity_types = \Drupal::config('multiversion.settings')
    ->get('enabled_entity_types') ?: [];
  if (empty($entity_types)) {
    return $this;
  }
  $migration = $this
    ->createMigration();
  $migration
    ->installDependencies();
  $this->eventDispatcher
    ->dispatch(MultiversionManagerEvents::PRE_MIGRATE, new MultiversionManagerEvent($entity_types, self::OP_ENABLE));
  $has_data = $this
    ->prepareContentForMigration($entity_types, $migration, self::OP_ENABLE);

  // Nasty workaround until {@link https://www.drupal.org/node/2549143 there
  // is a better way to invalidate caches in services}.
  // For some reason we have to clear cache on the "global" service as opposed
  // to the injected one. Services in the dark corners of Entity API won't see
  // the same result otherwise. Very strange.
  \Drupal::entityTypeManager()
    ->clearCachedDefinitions();
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();
  self::enableMigrationIsActive(array_keys($entity_types));
  $migration
    ->applyNewStorage(array_keys($entity_types));

  // Definitions will now be updated. So fetch the new ones.
  if ($entity_types_to_enable !== NULL) {
    $updated_entity_types = [];
    foreach ($entity_types as $entity_type_id => $entity_type) {
      $updated_entity_types[$entity_type_id] = $this->entityTypeManager
        ->getStorage($entity_type_id)
        ->getEntityType();
    }
  }
  else {
    $updated_entity_types = $this
      ->getSupportedEntityTypes();
  }

  // Temporarily disable the maintenance of the {comment_entity_statistics} table.
  $this->state
    ->set('comment.maintain_entity_statistics', FALSE);
  \Drupal::state()
    ->resetCache();
  foreach ($updated_entity_types as $entity_type_id => $entity_type) {

    // Migrate from the temporary storage to the new shiny home.
    if ($has_data[$entity_type_id]) {
      $field_map = $migration
        ->getFieldMap($entity_type, self::OP_ENABLE, self::FROM_TMP);
      $migration
        ->migrateContentFromTemp($entity_type, $field_map);
      $migration
        ->cleanupMigration($entity_type_id . '__' . self::TO_TMP);
      $migration
        ->cleanupMigration($entity_type_id . '__' . self::FROM_TMP);
    }

    // Mark the migration for this particular entity type as done even if no
    // actual content was migrated.
    $this->state
      ->set("multiversion.migration_done.{$entity_type_id}", TRUE);
  }
  foreach ($entity_types as $entity_type_id => $entity_type) {
    $enabled = $this->state
      ->get("multiversion.migration_done.{$entity_type_id}", FALSE);
    if (!in_array($entity_type_id, $enabled_entity_types) && $enabled) {
      $enabled_entity_types[] = $entity_type_id;
    }
  }
  \Drupal::configFactory()
    ->getEditable('multiversion.settings')
    ->set('enabled_entity_types', $enabled_entity_types)
    ->save();

  // Enable the the maintenance of entity statistics for comments.
  $this->state
    ->set('comment.maintain_entity_statistics', TRUE);

  // Clean up after us.
  $migration
    ->uninstallDependencies();
  self::enableMigrationIsActive(FALSE);

  // Mark the whole migration as done. Any entity types installed after this
  // will not need a migration since they will be created directly on top of
  // the Multiversion storage.
  $this->state
    ->set('multiversion.migration_done', TRUE);
  $this->eventDispatcher
    ->dispatch(MultiversionManagerEvents::POST_MIGRATE, new MultiversionManagerEvent($entity_types, self::OP_ENABLE));

  // Another nasty workaround because the cache is getting skewed somewhere.
  // And resetting the cache on the injected state service does not work.
  // Very strange.
  \Drupal::state()
    ->resetCache();
  return $this;
}