You are here

public function SearchApiAlgoliaBackend::deleteItems in Search API Algolia 8

Same name and namespace in other branches
  1. 3.0.x src/Plugin/search_api/backend/SearchApiAlgoliaBackend.php \Drupal\search_api_algolia\Plugin\search_api\backend\SearchApiAlgoliaBackend::deleteItems()
  2. 2.0.x src/Plugin/search_api/backend/SearchApiAlgoliaBackend.php \Drupal\search_api_algolia\Plugin\search_api\backend\SearchApiAlgoliaBackend::deleteItems()

Deletes the specified items from the index.

Parameters

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

string[] $item_ids: The IDs of the deleted items.

Throws

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

Overrides BackendSpecificInterface::deleteItems

File

src/Plugin/search_api/backend/SearchApiAlgoliaBackend.php, line 336

Class

SearchApiAlgoliaBackend
Class SearchApiAlgoliaBackend.

Namespace

Drupal\search_api_algolia\Plugin\search_api\backend

Code

public function deleteItems(IndexInterface $index, array $ids) {

  // Deleting all items included in the $ids array.
  if ($this->languageManager
    ->isMultilingual()) {
    foreach ($this->languageManager
      ->getLanguages() as $language) {
      try {

        // Connect to the Algolia index for specific language.
        $this
          ->connect($index, '', $language
          ->getId());
      } catch (\Exception $e) {
        $this
          ->getLogger()
          ->error('Failed to connect to Algolia index while deleting indexed items, Error: @message', [
          '@message' => $e
            ->getMessage(),
        ]);
        return;
      }
      $this
        ->getAlgoliaIndex()
        ->deleteObjects($ids);
    }
  }
  else {

    // Connect to the Algolia index.
    try {
      $this
        ->connect($index);
    } catch (\Exception $e) {
      $this
        ->getLogger()
        ->error('Failed to connect to Algolia index while deleting indexed items, Error: @message', [
        '@message' => $e
          ->getMessage(),
      ]);
      return;
    }
    $this
      ->getAlgoliaIndex()
      ->deleteObjects($ids);
  }
}