TaxonomyEntityIndexCommands.php in Taxonomy Entity Index 8
File
src/Commands/TaxonomyEntityIndexCommands.php
View source
<?php
namespace Drupal\taxonomy_entity_index\Commands;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drush\Commands\DrushCommands;
class TaxonomyEntityIndexCommands extends DrushCommands {
protected $entityTypeManager;
protected $configFactory;
protected $logger;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->configFactory = $config_factory;
$this->logger = $logger_factory
->get('taxonomy_entity_index');
}
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.');
}
}