You are here

public function TaskManager::addTask in Search API 8

Adds a new pending task.

In case this would duplicate an existing task, that existing task is returned instead.

Parameters

string $type: The type of task.

\Drupal\search_api\ServerInterface|null $server: (optional) The search server associated with the task, if any.

\Drupal\search_api\IndexInterface|null $index: (optional) The search index associated with the task, if any.

mixed|null $data: (optional) Additional, type-specific data to save with the task.

Return value

\Drupal\search_api\Task\TaskInterface The new task, or an identical existing task.

Overrides TaskManagerInterface::addTask

File

src/Task/TaskManager.php, line 133

Class

TaskManager
Provides a service for managing pending tasks.

Namespace

Drupal\search_api\Task

Code

public function addTask($type, ServerInterface $server = NULL, IndexInterface $index = NULL, $data = NULL) {
  $server_id = $server ? $server
    ->id() : NULL;
  $index_id = $index ? $index
    ->id() : NULL;
  if (isset($data)) {
    if ($data instanceof EntityInterface) {
      $data = [
        '#entity_type' => $data
          ->getEntityTypeId(),
        '#values' => $data
          ->toArray(),
      ];
    }
    $data = serialize($data);
  }
  $result = $this
    ->getTasksQuery([
    'type' => $type,
    'server_id' => $server_id,
    'index_id' => $index_id,
    'data' => $data,
  ])
    ->execute();
  if ($result) {
    return $this
      ->getTaskStorage()
      ->load(reset($result));
  }
  $task = $this
    ->getTaskStorage()
    ->create([
    'type' => $type,
    'server_id' => $server_id,
    'index_id' => $index_id,
    'data' => $data,
  ]);
  $task
    ->save();
  return $task;
}