You are here

public static function IndexBatchHelper::finish in Search API 8

Finishes an index batch.

File

src/IndexBatchHelper.php, line 182

Class

IndexBatchHelper
Provides helper methods for indexing items using Drupal's Batch API.

Namespace

Drupal\search_api

Code

public static function finish($success, $results, $operations) {

  // Check if the batch job was successful.
  if ($success) {

    // Display the number of items indexed.
    if (!empty($results['indexed'])) {

      // Build the indexed message.
      $indexed_message = static::formatPlural($results['indexed'], 'Successfully indexed 1 item.', 'Successfully indexed @count items.');

      // Notify user about indexed items.
      \Drupal::messenger()
        ->addStatus($indexed_message);

      // Display the number of items not indexed.
      if (!empty($results['not indexed'])) {

        // Build the not indexed message.
        $not_indexed_message = static::formatPlural($results['not indexed'], '1 item could not be indexed. Check the logs for details.', '@count items could not be indexed. Check the logs for details.');

        // Notify user about not indexed items.
        \Drupal::messenger()
          ->addWarning($not_indexed_message);
      }
    }
    else {

      // Notify user about failure to index items.
      \Drupal::messenger()
        ->addError(static::t("Couldn't index items. Check the logs for details."));
    }
  }
  else {

    // Notify user about batch job failure.
    \Drupal::messenger()
      ->addError(static::t('An error occurred while trying to index items. Check the logs for details.'));
  }
}