You are here

public function TestBackend::deleteAllIndexItems in Search API 8

Deletes all the items from the index.

Parameters

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

string|null $datasource_id: (optional) If given, only delete items from the datasource with the given ID.

Throws

\Drupal\search_api\SearchApiException Thrown if an error occurred while trying to delete indexed items.

Overrides BackendSpecificInterface::deleteAllIndexItems

File

tests/search_api_test/src/Plugin/search_api/backend/TestBackend.php, line 206

Class

TestBackend
Provides a dummy backend for testing purposes.

Namespace

Drupal\search_api_test\Plugin\search_api\backend

Code

public function deleteAllIndexItems(IndexInterface $index, $datasource_id = NULL) {
  if ($override = $this
    ->getMethodOverride(__FUNCTION__)) {
    call_user_func($override, $this, $index, $datasource_id);
    return;
  }
  $this
    ->checkError(__FUNCTION__);
  $key = 'search_api_test.backend.indexed.' . $index
    ->id();
  if (!$datasource_id) {
    \Drupal::state()
      ->delete($key);
    return;
  }
  $indexed = \Drupal::state()
    ->get($key, []);

  /** @var \Drupal\search_api\Item\ItemInterface $item */
  foreach (array_keys($indexed) as $item_id) {
    list($item_datasource_id) = Utility::splitCombinedId($item_id);
    if ($item_datasource_id == $datasource_id) {
      unset($indexed[$item_id]);
    }
  }
  \Drupal::state()
    ->set($key, $indexed);
}