You are here

public function SearchApiCombinedEntityDataSourceController::loadItems in Search API 7

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 SearchApiDataSourceControllerInterface::loadItems

File

includes/datasource_multiple.inc, line 31
Contains SearchApiCombinedEntityDataSourceController.

Class

SearchApiCombinedEntityDataSourceController
Provides a datasource for indexing multiple types of entities.

Code

public function loadItems(array $ids) {
  $ids_by_type = array();
  foreach ($ids as $id) {
    list($type, $entity_id) = explode('/', $id);
    $ids_by_type[$type][$entity_id] = $id;
  }
  $items = array();
  foreach ($ids_by_type as $type => $type_ids) {
    foreach (entity_load($type, array_keys($type_ids)) as $entity_id => $entity) {
      $id = $type_ids[$entity_id];
      $item = (object) array(
        $type => $entity,
      );
      $item->item_id = $id;
      $item->item_type = $type;
      $item->item_entity_id = $entity_id;
      $item->item_bundle = NULL;

      // Add the item language so the "search_api_language" field will work
      // correctly.
      $item->language = isset($entity->language) ? $entity->language : NULL;
      try {
        list(, , $bundle) = entity_extract_ids($type, $entity);
        $item->item_bundle = $bundle ? "{$type}:{$bundle}" : NULL;
      } catch (EntityMalformedException $e) {

        // Will probably make problems at some other place, but for extracting
        // the bundle it is really not critical enough to fail on – just
        // ignore this exception.
      }
      $items[$id] = $item;
      unset($type_ids[$entity_id]);
    }
    if ($type_ids) {
      search_api_track_item_delete($type, array_keys($type_ids));
    }
  }
  return $items;
}