function views_plugin_query_default::get_result_entities

3.x views_plugin_query_default.inc views_plugin_query_default::get_result_entities($results, $relationship = NULL)

Returns the according entity objects for the given query results.

Overrides views_plugin_query::get_result_entities

File

plugins/views_plugin_query_default.inc, line 1596
Defines the default query object.

Class

views_plugin_query_default
Object used to create a SELECT query.

Code

function get_result_entities($results, $relationship = NULL) {
  $base_table = $this->base_table;
  $base_table_alias = $base_table;

  if (!empty($relationship)) {
    foreach ($this->view->relationship as $current) {
      if ($current->alias == $relationship) {
        $base_table = $current->definition['base'];
        $base_table_alias = $relationship;
        break;
      }
    }
  }
  $table_data = views_fetch_data($base_table);

  // Bail out if the table has not specified the according entity-type.
  if (!isset($table_data['table']['entity type'])) {
    return FALSE;
  }
  $entity_type = $table_data['table']['entity type'];
  $info = entity_get_info($entity_type);
  $id_alias = $this->get_field_alias($base_table_alias, $info['entity keys']['id']);

  // Assemble the ids of the entities to load.
  $ids = array();
  foreach ($results as $key => $result) {
    if (isset($result->$id_alias)) {
      $ids[$key] = $result->$id_alias;
    }
  }

  $entities = entity_load($entity_type, $ids);
  // Re-key the array by row-index.
  $result = array();
  foreach ($ids as $key => $id) {
    $result[$key] = isset($entities[$id]) ? $entities[$id] : FALSE;
  }
  return array($entity_type, $result);
}