1 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

core/modules/views/plugins/views_plugin_query_default.inc, line 1595
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);
  $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) {
      // @todo: Switch this to entity_revision_load() if that function gets
      // into core.
      // See: https://github.com/backdrop/backdrop-issues/issues/3777
      $entity_info = entity_get_info($entity_type);
      if (!empty($entity_info['entity keys']['revision'])) {
        $entity_revisions = entity_load_multiple($entity_type, FALSE, array($entity_info['entity keys']['revision'] => $id));
        $result[$key] = reset($entity_revisions);
      }
      else {
        $result[$key] = FALSE;
      }
    }
  }

  return array($entity_type, $result);
}