You are here

public function MultiversionManager::enableEntityTypes in Multiversion 8.2

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

Return value

\Drupal\multiversion\MultiversionManagerInterface

Overrides MultiversionManagerInterface::enableEntityTypes

File

src/MultiversionManager.php, line 231

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();
  if (empty($entity_types)) {
    return $this;
  }

  // Temporarily disable the maintenance of the {comment_entity_statistics} table.
  $this->state
    ->set('comment.maintain_entity_statistics', FALSE);
  $multiversion_settings = \Drupal::configFactory()
    ->getEditable('multiversion.settings');
  $enabled_entity_types = $multiversion_settings
    ->get('enabled_entity_types') ?: [];
  $operations = [];
  $sandbox = [];

  // Define the step size.
  $sandbox['step_size'] = Settings::get('entity_conversion_batch_size', 50);
  foreach ($entity_types as $entity_type_id => $entity_type) {
    if (in_array($entity_type_id, $enabled_entity_types)) {
      continue;
    }
    $base_table = $entity_type
      ->getBaseTable();
    $sandbox['base_tables'][$entity_type_id] = $base_table;
    $entities_count = $this->connection
      ->select($base_table)
      ->countQuery()
      ->execute()
      ->fetchField();
    $i = 0;
    while ($i <= $entities_count) {
      $operations[] = [
        [
          get_class($this),
          'convertToMultiversionable',
        ],
        [
          $entity_type_id,
          $this->entityTypeManager,
          $this->state,
          $multiversion_settings,
          &$sandbox,
        ],
      ];
      $i += $sandbox['step_size'];
    }
    $operations[] = [
      [
        get_class($this),
        'fixPrimaryKeys',
      ],
      [
        $entity_type_id,
        $this->entityTypeManager,
        $this->connection,
      ],
    ];
  }

  // Create and process the batch.
  if (!empty($operations)) {
    $batch = [
      'operations' => $operations,
      'finished' => [
        get_class($this),
        'conversionFinished',
      ],
    ];
    batch_set($batch);
    $batch =& batch_get();
    $batch['progressive'] = FALSE;
    batch_process();
  }

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