You are here

function Sql::load_entities in Views (for Drupal 7) 8.3

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::load_entities

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

File

lib/Drupal/views/Plugin/views/query/Sql.php, line 1583
Definition of Drupal\views\Plugin\views\query\Sql.

Class

Sql
@todo.

Namespace

Drupal\views\Plugin\views\query

Code

function load_entities(&$results) {
  $entity_tables = $this
    ->get_entity_tables();

  // 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
      ->get_field_alias($table_alias, $id_key);
    foreach ($results as $index => $result) {

      // Store the entity id if it was found.
      if (!empty($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;
      }
    }
  }
}