function Sql::loadEntities

Loads all entities contained in the passed-in $results. . If the entity belongs to the base table, then it gets stored in $result->_entity. Otherwise, it gets stored in $result->_relationship_entities[$relationship_id];

Overrides QueryPluginBase::loadEntities

1 call to Sql::loadEntities()
Sql::execute in drupal/core/modules/views/lib/Drupal/views/Plugin/views/query/Sql.php
Executes the query and fills the associated view object with according values.

File

drupal/core/modules/views/lib/Drupal/views/Plugin/views/query/Sql.php, line 1516
Definition of Drupal\views\Plugin\views\query\Sql.

Class

Sql
@todo.

Namespace

Drupal\views\Plugin\views\query

Code

function loadEntities(&$results) {
  $entity_tables = $this
    ->getEntityTables();

  // No entity tables found, nothing else to do here.
  if (empty($entity_tables)) {
    return;
  }

  // Initialize the entity placeholders in $results.
  foreach ($results as $index => $result) {
    $results[$index]->_entity = FALSE;
    $results[$index]->_relationship_entities = array();
  }

  // Assemble a list of entities to load.
  $ids_by_table = array();
  foreach ($entity_tables as $table_alias => $table) {
    $entity_type = $table['entity_type'];
    $info = entity_get_info($entity_type);
    $id_key = empty($table['revision']) ? $info['entity_keys']['id'] : $info['entity_keys']['revision'];
    $id_alias = $this
      ->getFieldAlias($table_alias, $id_key);
    foreach ($results as $index => $result) {

      // Store the entity id if it was found.
      if (isset($result->{$id_alias}) && $result->{$id_alias} != '') {
        $ids_by_table[$table_alias][$index] = $result->{$id_alias};
      }
    }
  }

  // Load all entities and assign them to the correct result row.
  foreach ($ids_by_table as $table_alias => $ids) {
    $table = $entity_tables[$table_alias];
    $entity_type = $table['entity_type'];
    $relationship_id = $table['relationship_id'];

    // Drupal core currently has no way to load multiple revisions. Sad.
    if ($table['revision']) {
      $entities = array();
      foreach ($ids as $index => $revision_id) {
        $entity = entity_revision_load($entity_type, $revision_id);
        if ($entity) {
          $entities[$revision_id] = $entity;
        }
      }
    }
    else {
      $entities = entity_load_multiple($entity_type, $ids);
    }
    foreach ($ids as $index => $id) {
      $entity = isset($entities[$id]) ? $entities[$id] : FALSE;
      if ($relationship_id == 'none') {
        $results[$index]->_entity = $entity;
      }
      else {
        $results[$index]->_relationship_entities[$relationship_id] = $entity;
      }
    }
  }
}