You are here

public function SearchApiAlgoliaBackend::indexItems in Search API Algolia 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/search_api/backend/SearchApiAlgoliaBackend.php \Drupal\search_api_algolia\Plugin\search_api\backend\SearchApiAlgoliaBackend::indexItems()
  2. 3.0.x src/Plugin/search_api/backend/SearchApiAlgoliaBackend.php \Drupal\search_api_algolia\Plugin\search_api\backend\SearchApiAlgoliaBackend::indexItems()
1 call to SearchApiAlgoliaBackend::indexItems()
SearchApiAlgoliaBackend::indexItem in src/Plugin/search_api/backend/SearchApiAlgoliaBackend.php
Indexes a single item on the specified index.

File

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

Class

SearchApiAlgoliaBackend
Class SearchApiAlgoliaBackend.

Namespace

Drupal\search_api_algolia\Plugin\search_api\backend

Code

public function indexItems(IndexInterface $index, array $items) {
  $objects = [];

  /** @var \Drupal\search_api\Item\ItemInterface[] $items */
  foreach ($items as $id => $item) {
    $objects[$id] = $this
      ->prepareItem($index, $item);
  }

  // Let other modules alter objects before sending them to Algolia.
  $this
    ->alterAlgoliaObjects($objects, $index, $items);
  if (count($objects) > 0) {
    $itemsToIndex = [];
    if ($this->languageManager
      ->isMultilingual()) {
      foreach ($objects as $item) {
        $itemsToIndex[$item['search_api_language']][] = $item;
      }
    }
    else {
      $itemsToIndex[''] = $objects;
    }
    foreach ($itemsToIndex as $language => $items) {

      // Allow adding objects to logs for investigation.
      if ($this
        ->isDebugActive()) {
        foreach ($items as $item) {
          $this
            ->getLogger()
            ->notice('Data pushed to Algolia for Language @language : @data', [
            '@data' => json_encode($item),
            '@language' => $language,
          ]);
        }
      }
      try {
        $this
          ->connect($index, '', $language);
        $this
          ->getAlgoliaIndex()
          ->saveObjects($items);
      } catch (AlgoliaException $e) {
        $this
          ->getLogger()
          ->warning(Html::escape($e
          ->getMessage()));
      }
    }
  }
  return array_keys($objects);
}