You are here

public function SarniaController::load in Sarnia 7

Implements DrupalEntityControllerInterface::load().

Overrides DrupalDefaultEntityController::load

File

./sarnia.module, line 530

Class

SarniaController
Controller class for Sarnia entities.

Code

public function load($ids = array(), $conditions = array()) {

  // Get cached entities.
  $entities = $this
    ->cacheGet($ids, $conditions);

  // Load un-cached entities. In general, this will not need to load any
  // entities, since when Solr queries are run from Views,
  // SarniaSolrService::postQuery() has already stashed all of the loaded
  // entities.
  if ($ids === FALSE || ($load_ids = array_diff($ids, array_keys($entities)))) {

    // Load the Search API index to query.
    $index = search_api_index_load($this->entityInfo['bundles'][$this->entityType]['search_api_index']);

    // Disable facets when loading entities.
    $index
      ->server()
      ->disableFeature('search_api_facets');

    // Create a Search API query.
    $query = $index
      ->query(array(
      'parse mode' => 'direct',
      'sarnia use raw keys' => TRUE,
    ));

    // Result field containing the entity id.
    $id_field = $this->entityInfo['bundles'][$this->entityType]['id_field'];
    do {

      // Build the query string. We use search keys instead of filter queries
      // because they're cached differently by Solr. This loads items in
      // batches of 200.
      if (is_array($load_ids)) {
        $keys = array();
        $count = 0;
        foreach ($load_ids as $i => $id) {
          $keys[] = "{$id_field}:{$id}";
          unset($load_ids[$i]);
          $count++;
          if ($count >= 200) {
            break;
          }
        }
        $query
          ->keys(implode(' OR ', $keys));
      }

      // Run the query. We don't have to do anything beyond querying, since
      // SarniaSolrService::postQuery() stashes all entity results.
      // @todo check for entities that DON'T load, and cache those as FALSE.
      $query
        ->execute();
    } while (is_array($load_ids) && !empty($load_ids));

    // Retrieve entities from the cache, where ::stash() puts them after each
    // query.
    $entities = $this
      ->cacheGet($ids, $conditions);

    // Un-disable facets in case the server object is used again later.
    $index
      ->server()
      ->unDisableFeature('search_api_facets');
  }
  return $entities;
}