You are here

public function Index::loadItemsMultiple in Search API 8

Loads multiple search objects for this index.

Parameters

array $item_ids: The internal item IDs of the objects, with datasource prefix.

Return value

\Drupal\Core\TypedData\ComplexDataInterface[] The loaded items, keyed by their internal item IDs.

Overrides IndexInterface::loadItemsMultiple

2 calls to Index::loadItemsMultiple()
Index::indexItems in src/Entity/Index.php
Indexes a set amount of items.
Index::loadItem in src/Entity/Index.php
Loads a single search object of this index.

File

src/Entity/Index.php, line 864

Class

Index
Defines the search index configuration entity.

Namespace

Drupal\search_api\Entity

Code

public function loadItemsMultiple(array $item_ids) {

  // Group the requested items by datasource. This will also later be used to
  // determine whether all items were loaded successfully.
  $items_by_datasource = [];
  foreach ($item_ids as $item_id) {
    list($datasource_id, $raw_id) = Utility::splitCombinedId($item_id);
    $items_by_datasource[$datasource_id][$raw_id] = $item_id;
  }

  // Load the items from the datasources and keep track of which were
  // successfully retrieved.
  $items = [];
  foreach ($items_by_datasource as $datasource_id => $raw_ids) {
    try {
      $datasource = $this
        ->getDatasource($datasource_id);
      $datasource_items = $datasource
        ->loadMultiple(array_keys($raw_ids));
      foreach ($datasource_items as $raw_id => $item) {
        $id = $raw_ids[$raw_id];
        $items[$id] = $item;

        // Remember that we successfully loaded this item.
        unset($items_by_datasource[$datasource_id][$raw_id]);
      }
    } catch (SearchApiException $e) {
      $this
        ->logException($e);

      // If the complete datasource could not be loaded, don't report all its
      // individual requested items as missing.
      unset($items_by_datasource[$datasource_id]);
    }
  }

  // Check whether there are requested items that couldn't be loaded.
  $items_by_datasource = array_filter($items_by_datasource);
  if ($items_by_datasource) {

    // Extract the second-level values of the two-dimensional array (that is,
    // the combined item IDs) and log a warning reporting their absence.
    $missing_ids = array_reduce(array_map('array_values', $items_by_datasource), 'array_merge', []);
    $args['%index'] = $this
      ->label();
    $args['@items'] = '"' . implode('", "', $missing_ids) . '"';
    $this
      ->getLogger()
      ->warning('Could not load the following items on index %index: @items.', $args);

    // Also remove those items from tracking so we don't keep trying to load
    // them.
    foreach ($items_by_datasource as $datasource_id => $raw_ids) {
      $this
        ->trackItemsDeleted($datasource_id, array_keys($raw_ids));
    }
  }

  // Return the loaded items.
  return $items;
}