You are here

public function IndexTaskManager::trackItems in Search API 8

Tracks items according to the given event.

Parameters

\Drupal\search_api\Task\TaskEvent $event: The task event.

File

src/Task/IndexTaskManager.php, line 130

Class

IndexTaskManager
Provides a service for managing pending index tasks.

Namespace

Drupal\search_api\Task

Code

public function trackItems(TaskEvent $event) {
  $event
    ->stopPropagation();
  $task = $event
    ->getTask();
  $index = $task
    ->getIndex();
  if (!$index
    ->hasValidTracker()) {
    $args['%index'] = $index
      ->label();
    $message = new FormattableMarkup('Index %index does not have a valid tracker set.', $args);
    $event
      ->setException(new SearchApiException($message));
    return;
  }
  $data = $task
    ->getData();
  $datasource_id = $data['datasource'];
  $reschedule = FALSE;
  if ($index
    ->isValidDatasource($datasource_id)) {
    $raw_ids = $index
      ->getDatasource($datasource_id)
      ->getItemIds($data['page']);
    if ($raw_ids !== NULL) {
      $reschedule = TRUE;
      if ($raw_ids) {
        $index
          ->startBatchTracking();
        $index
          ->trackItemsInserted($datasource_id, $raw_ids);
        $index
          ->stopBatchTracking();
      }
    }
  }
  if ($reschedule) {
    ++$data['page'];
    $this->taskManager
      ->addTask(self::TRACK_ITEMS_TASK_TYPE, NULL, $index, $data);
  }
}