You are here

public function TaxonomyEntityIndexCommands::rebuild in Taxonomy Entity Index 8

Rebuild taxonomy entity index.

@command taxonomy_entity_index:rebuild @aliases tei:rebuild, taxonomy-entity-index-rebuild, tei-rebuild @usage drush taxonomy-entity-index-rebuild Reindex all configured entity types. @usage drush taxonomy-entity-index-rebuild node,media Reindex only node and media entities.

Parameters

string $entity_types: A comma separated list of entity types to reindex.

File

src/Commands/TaxonomyEntityIndexCommands.php, line 66

Class

TaxonomyEntityIndexCommands
A Drush commandfile.

Namespace

Drupal\taxonomy_entity_index\Commands

Code

public function rebuild($entity_types = '') {
  if (empty($entity_types)) {
    $config = $this->configFactory
      ->get('taxonomy_entity_index.settings');
    $entity_types = $config
      ->get('types');
  }
  else {
    $entity_types = explode(',', $entity_types);
  }
  $this->logger
    ->info('Starting reindex for entity types: @types', [
    '@types' => implode(',', $entity_types),
  ]);
  $operations = [];
  $numOperations = 0;
  $batchId = 1;
  foreach ($entity_types as $type) {
    try {
      $storage = $this->entityTypeManager
        ->getStorage($type);
      $query = $storage
        ->getQuery();
      $ids = $query
        ->execute();
    } catch (\Exception $e) {
      $this
        ->output()
        ->writeln($e);
      $this->logger
        ->warning('Error found @e', [
        '@e' => $e,
      ]);
    }
    if (!empty($ids)) {
      foreach ($ids as $id) {
        $this
          ->output()
          ->writeln('Preparing batch: ' . $batchId);
        $operations[] = [
          '\\Drupal\\taxonomy_entity_index\\BatchService::processEntityTypeReindex',
          [
            $batchId,
            $type,
            $id,
          ],
        ];
        $batchId++;
        $numOperations++;
      }
    }
    else {
      $this->logger
        ->warning('No entities of this type @type', [
        '@type' => $type,
      ]);
    }
  }
  $batch = [
    'title' => t('Updating @num item(s)', [
      '@num' => $numOperations,
    ]),
    'operations' => $operations,
    'finished' => '\\Drupal\\taxonomy_entity_index\\BatchService::processEntityTypeReindexFinished',
  ];
  batch_set($batch);
  drush_backend_batch_process();
  $this->logger
    ->notice("Batch operations end.");
  $this->logger
    ->info('Update batch operations end.');
}