You are here

class BatchService in Taxonomy Entity Index 8

Class BatchService.

Hierarchy

Expanded class hierarchy of BatchService

File

src/BatchService.php, line 8

Namespace

Drupal\taxonomy_entity_index
View source
class BatchService {

  /**
   * Batch process callback.
   *
   * @param int $batch_id
   *   ID of the batch.
   * @param string $entity_type
   *   The entity type.
   * @param int $entity_id
   *   The entity ID.
   * @param object $context
   *   Context for operations.
   */
  public function processEntityTypeReindex($batch_id, $entity_type, $entity_id, &$context) {
    $context['results'][] = $batch_id;
    $context['message'] = t('Running Batch @batch_id: type: @entity_type - id: @entity_id', [
      '@batch_id' => $batch_id,
      '@entity_type' => $entity_type,
      '@entity_id' => $entity_id,
    ]);
    $entity = \Drupal::entityTypeManager()
      ->getStorage($entity_type)
      ->load($entity_id);
    taxonomy_entity_index_entity_update($entity);
  }

  /**
   * Batch Finished callback.
   *
   * @param bool $success
   *   Success of the operation.
   * @param array $results
   *   Array of results for post processing.
   * @param array $operations
   *   Array of operations.
   */
  public function processEntityTypeReindexFinished($success, array $results, array $operations) {
    $messenger = \Drupal::messenger();
    if ($success) {

      // Here we could do something meaningful with the results.
      // We just display the number of nodes we processed...
      $messenger
        ->addMessage(t('@count results processed.', [
        '@count' => count($results),
      ]));
    }
    else {

      // An error occurred.
      // $operations contains the operations that remained unprocessed.
      $error_operation = reset($operations);
      $messenger
        ->addMessage(t('An error occurred while processing @operation with arguments : @args', [
        '@operation' => $error_operation[0],
        '@args' => print_r($error_operation[0], TRUE),
      ]));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BatchService::processEntityTypeReindex public function Batch process callback.
BatchService::processEntityTypeReindexFinished public function Batch Finished callback.