You are here

class TaxonomyEntityIndexCommands in Taxonomy Entity Index 8

A Drush commandfile.

Hierarchy

Expanded class hierarchy of TaxonomyEntityIndexCommands

1 string reference to 'TaxonomyEntityIndexCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses TaxonomyEntityIndexCommands
taxonomy_entity_index.commands in ./drush.services.yml
Drupal\taxonomy_entity_index\Commands\TaxonomyEntityIndexCommands

File

src/Commands/TaxonomyEntityIndexCommands.php, line 13

Namespace

Drupal\taxonomy_entity_index\Commands
View source
class TaxonomyEntityIndexCommands extends DrushCommands {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * A logger instance.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
   */
  protected $logger;

  /**
   * Constructs a drush command object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
   *   The logger factory service.
   */
  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');
  }

  /**
   * Rebuild taxonomy entity index.
   *
   * @param string $entity_types
   *   A comma separated list of entity types to
   *   reindex.
   *
   * @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.
   */
  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.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TaxonomyEntityIndexCommands::$configFactory protected property The config factory.
TaxonomyEntityIndexCommands::$entityTypeManager protected property The entity type manager.
TaxonomyEntityIndexCommands::$logger protected property A logger instance.
TaxonomyEntityIndexCommands::rebuild public function Rebuild taxonomy entity index.
TaxonomyEntityIndexCommands::__construct public function Constructs a drush command object.