You are here

function multiversion_update_8004 in Multiversion 8

Update user entity type to non-multiversionable.

File

./multiversion.install, line 202

Code

function multiversion_update_8004() {
  $update_manager = \Drupal::entityDefinitionUpdateManager();
  $changes = $update_manager
    ->getChangeSummary();

  // Check if user entity type has new changes.
  if (isset($changes['user'])) {

    /** @var \Drupal\Core\Entity\EntityTypeManager $entity_type_manager */
    $entity_type_manager = \Drupal::service('entity_type.manager');
    $storage = $entity_type_manager
      ->getStorage('user');
    $entity_type = $storage
      ->getEntityType();
    $database = \Drupal::database();
    try {
      $entity_type_manager = \Drupal::entityTypeManager();

      // Create a new migration and migrate user entities to the temporary storage.
      $migration = MultiversionMigration::create(\Drupal::getContainer(), $entity_type_manager);
      $migration
        ->installDependencies();
      $field_map = $migration
        ->getFieldMap($entity_type, MultiversionManager::OP_DISABLE, MultiversionManager::TO_TMP);
      $migration
        ->migrateContentToTemp($entity_type, $field_map);

      // Remove all data from the old storage.
      $migration
        ->emptyOldStorage($storage);

      // Delete revision tables that after applying updates doesn't get deleted.
      $tables_to_delete = [
        'user_revision__roles',
        'user_revision__user_picture',
      ];
      foreach ($tables_to_delete as $table_name) {
        if ($database
          ->schema()
          ->tableExists($table_name)) {
          $database
            ->schema()
            ->dropTable($table_name);
        }
      }

      // Apply new updates.
      $migration
        ->applyNewStorage([
        'user',
      ]);

      // Migrate content from the temporary storage to the new storage.
      $field_map = $migration
        ->getFieldMap($entity_type, MultiversionManager::OP_DISABLE, MultiversionManager::FROM_TMP);
      $migration
        ->migrateContentFromTemp($entity_type, $field_map);
      $migration
        ->cleanupMigration('user__to_tmp');
      $migration
        ->cleanupMigration('user__from_tmp');
      $migration
        ->uninstallDependencies();
    } catch (\Exception $e) {
      \Drupal::logger('multiversion')
        ->error($e
        ->getMessage());
    }
  }
}