You are here

public function MultiversionManager::disableEntityTypes in Multiversion 8

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

Return value

\Drupal\multiversion\MultiversionManagerInterface

Overrides MultiversionManagerInterface::disableEntityTypes

File

src/MultiversionManager.php, line 360

Class

MultiversionManager

Namespace

Drupal\multiversion

Code

public function disableEntityTypes($entity_types_to_disable = NULL) {
  $entity_types = $entity_types_to_disable !== NULL ? $entity_types_to_disable : $this
    ->getEnabledEntityTypes();
  $migration = $this
    ->createMigration();
  $migration
    ->installDependencies();
  $this->eventDispatcher
    ->dispatch(MultiversionManagerEvents::PRE_MIGRATE, new MultiversionManagerEvent($entity_types, self::OP_DISABLE));
  $has_data = $this
    ->prepareContentForMigration($entity_types, $migration, self::OP_DISABLE);
  if (empty($entity_types)) {
    return $this;
  }
  if ($entity_types_to_disable === NULL) {

    // Uninstall field storage definitions provided by multiversion.
    $this->entityTypeManager
      ->clearCachedDefinitions();
    $update_manager = \Drupal::entityDefinitionUpdateManager();
    foreach ($this->entityTypeManager
      ->getDefinitions() as $entity_type) {
      if ($entity_type
        ->entityClassImplements(FieldableEntityInterface::class)) {
        $entity_type_id = $entity_type
          ->id();
        $revision_key = $entity_type
          ->getKey('revision');

        /** @var \Drupal\Core\Entity\FieldableEntityStorageInterface $storage */
        $storage = $this->entityTypeManager
          ->getStorage($entity_type_id);
        foreach ($this->entityFieldManager
          ->getFieldStorageDefinitions($entity_type_id) as $storage_definition) {

          // @todo We need to trigger field purging here.
          //   See https://www.drupal.org/node/2282119.
          if ($storage_definition
            ->getProvider() == 'multiversion' && !$storage
            ->countFieldData($storage_definition, TRUE) && $storage_definition
            ->getName() != $revision_key) {
            $update_manager
              ->uninstallFieldStorageDefinition($storage_definition);
          }
        }
      }
    }
  }
  $enabled_entity_types = \Drupal::config('multiversion.settings')
    ->get('enabled_entity_types') ?: [];
  foreach ($entity_types as $entity_type_id => $entity_type) {
    if (($key = array_search($entity_type_id, $enabled_entity_types)) !== FALSE) {
      unset($enabled_entity_types[$key]);
    }
  }
  if ($entity_types_to_disable === NULL) {
    $enabled_entity_types = [];
  }
  \Drupal::configFactory()
    ->getEditable('multiversion.settings')
    ->set('enabled_entity_types', $enabled_entity_types)
    ->save();
  \Drupal::entityTypeManager()
    ->clearCachedDefinitions();
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();
  self::disableMigrationIsActive(array_keys($entity_types));
  $migration
    ->applyNewStorage(array_keys($entity_types));

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

  // Definitions will now be updated. So fetch the new ones.
  $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();
  }
  foreach ($updated_entity_types as $entity_type_id => $entity_type) {

    // Drop unique key from uuid on each entity type.
    $base_table = $entity_type
      ->getBaseTable();
    $uuid_key = $entity_type
      ->getKey('uuid');
    $this->connection
      ->schema()
      ->dropUniqueKey($base_table, $entity_type_id . '_field__' . $uuid_key . '__value');

    // Migrate from the temporary storage to the drupal default storage.
    if ($has_data[$entity_type_id]) {
      $field_map = $migration
        ->getFieldMap($entity_type, self::OP_DISABLE, 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);
    }
    $this->state
      ->delete("multiversion.migration_done.{$entity_type_id}");
  }

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

  // Clean up after us.
  $migration
    ->uninstallDependencies();
  self::disableMigrationIsActive(FALSE);
  $this->state
    ->delete('multiversion.migration_done');
  $this->eventDispatcher
    ->dispatch(MultiversionManagerEvents::POST_MIGRATE, new MultiversionManagerEvent($entity_types, self::OP_DISABLE));
  return $this;
}