You are here

public static function IndexBatchHelper::create in Search API 8

Creates an indexing batch for a given search index.

Parameters

\Drupal\search_api\IndexInterface $index: The search index for which items should be indexed.

int|null $batch_size: (optional) Number of items to index per batch. Defaults to the cron limit set for the index.

int $limit: (optional) Maximum number of items to index. Defaults to indexing all remaining items.

Throws

\Drupal\search_api\SearchApiException Thrown if the batch could not be created.

2 calls to IndexBatchHelper::create()
CommandHelper::indexItemsToIndexCommand in src/Utility/CommandHelper.php
Indexes items on one or more indexes.
IndexStatusForm::submitForm in src/Form/IndexStatusForm.php
Form submission handler.

File

src/IndexBatchHelper.php, line 75

Class

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

Namespace

Drupal\search_api

Code

public static function create(IndexInterface $index, $batch_size = NULL, $limit = -1) {

  // Check if the size should be determined by the index cron limit option.
  if ($batch_size === NULL) {

    // Use the size set by the index.
    $batch_size = $index
      ->getOption('cron_limit', \Drupal::config('search_api.settings')
      ->get('default_cron_limit'));
  }

  // Check if indexing items is allowed.
  if ($index
    ->status() && !$index
    ->isReadOnly() && $batch_size !== 0 && $limit !== 0) {

    // Define the search index batch definition.
    $batch_definition = [
      'operations' => [
        [
          [
            __CLASS__,
            'process',
          ],
          [
            $index,
            $batch_size,
            $limit,
          ],
        ],
      ],
      'finished' => [
        __CLASS__,
        'finish',
      ],
      'progress_message' => static::t('Completed about @percentage% of the indexing operation (@current of @total).'),
    ];

    // Schedule the batch.
    batch_set($batch_definition);
  }
  else {
    $index_label = $index
      ->label();
    throw new SearchApiException("Failed to create a batch with batch size '{$batch_size}' and limit '{$limit}' for index '{$index_label}'.");
  }
}