You are here

public function views_plugin_query_default::get_result_entities in Views (for Drupal 7) 7.3

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 1643
Definition of views_plugin_query_default.

Class

views_plugin_query_default
Object used to create a SELECT query.

Code

public 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 (isset($current->alias) && $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);
  $is_revision = !empty($table_data['table']['revision']);
  $id_alias = $this
    ->get_field_alias($base_table_alias, $info['entity keys'][$is_revision ? 'revision' : '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};
    }
  }
  if (!$is_revision) {
    $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;
    }
  }
  else {

    // There's no way in core to load revisions in bulk.
    $result = array();
    foreach ($ids as $key => $id) {

      // Nodes can be dealt with in core.
      if ($entity_type == 'node') {
        $result[$key] = node_load(NULL, $id);
      }
      elseif (module_exists('entity')) {
        $result[$key] = entity_revision_load($entity_type, $id);
      }
      else {

        // Otherwise this isn't supported.
        watchdog('views', 'Attempt to load a revision on an unsupported entity type @entity_type.', array(
          '@entity_type' => $entity_type,
        ), WATCHDOG_WARNING);
      }
    }
  }
  return array(
    $entity_type,
    $result,
  );
}