You are here

public function SearchApiEtDatasourceController::loadItems in Search API Entity Translation 7.2

Loads items of the type of this data source controller.

Parameters

array $ids: The IDs of the items to load.

Return value

array The loaded items, keyed by ID.

Throws

SearchApiDataSourceException If any error state was encountered.

Overrides SearchApiEntityDataSourceController::loadItems

File

includes/SearchApiEtDatasourceController.php, line 35
Contains the SearchApiEtDatasourceController class.

Class

SearchApiEtDatasourceController
Provides multilingual versions of all entity types.

Code

public function loadItems(array $ids) {
  $item_languages = array();
  foreach ($ids as $id) {

    // This method might receive two different types of item IDs depending on
    // where it is being called from. For example, when called from
    // search_api_index_specific_items(), it will receive multilingual IDs
    // (with language prefix, like "2/en"). On the other hand, when called from
    // a processor (for example from SearchApiHighlight::getFulltextFields()),
    // the IDs won't be multilingual (no language prefix), just standard
    // entity IDs instead. Therefore we need to account for both cases here.
    // Case 1 - language is in item ID.
    if (SearchApiEtHelper::isValidItemId($id)) {
      $entity_id = SearchApiEtHelper::splitItemId($id, SearchApiEtHelper::ITEM_ID_ENTITY_ID);
      $item_languages[$entity_id][] = SearchApiEtHelper::splitItemId($id, SearchApiEtHelper::ITEM_ID_LANGUAGE);
    }
    else {
      $item_languages[$id][] = NULL;
    }
  }
  $entities = entity_load($this->entityType, array_keys($item_languages));

  // If some items could not be loaded, remove them from tracking.
  if (count($entities) != count($item_languages)) {
    $unknown = array_keys(array_diff_key($item_languages, $entities));
    if ($unknown) {
      $deleted = array();
      foreach ($unknown as $entity_id) {
        foreach ($item_languages[$entity_id] as $language) {
          $deleted[] = SearchApiEtHelper::buildItemId($entity_id, $language);
        }
      }
      search_api_track_item_delete($this->type, $deleted);
    }
  }

  // Now arrange them according to our IDs again, with language.
  $items = array();
  foreach ($item_languages as $entity_id => $languages) {
    if (!empty($entities[$entity_id])) {
      foreach ($languages as $language) {

        // Following on the two cases described above, we should return
        // the same item IDs (with or without language prefix) as received.
        $entity = clone $entities[$entity_id];
        $id = !empty($language) ? SearchApiEtHelper::buildItemId($entity_id, $language) : $entity_id;
        $entity->search_api_et_id = $id;

        // Keep current entity language if language is NULL.
        $entity->language = !is_null($language) ? $language : $entity->language;
        $items[$id] = $entity;
      }
    }
  }
  return $items;
}