You are here

public function Sql::loadEntities in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Plugin/views/query/Sql.php \Drupal\views\Plugin\views\query\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];

Parameters

\Drupal\views\ResultRow[] $results: The result of the SQL query.

Overrides QueryPluginBase::loadEntities

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

File

core/modules/views/src/Plugin/views/query/Sql.php, line 1478
Contains \Drupal\views\Plugin\views\query\Sql.

Class

Sql
Views query plugin for an SQL query.

Namespace

Drupal\views\Plugin\views\query

Code

public function loadEntities(&$results) {
  $entity_information = $this
    ->getEntityTableInfo();

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

  // Extract all entity types from entity_information.
  $entity_types = array();
  foreach ($entity_information as $info) {
    $entity_type = $info['entity_type'];
    if (!isset($entity_types[$entity_type])) {
      $entity_types[$entity_type] = \Drupal::entityManager()
        ->getDefinition($entity_type);
    }
  }

  // Assemble a list of entities to load.
  $ids_by_type = array();
  foreach ($entity_information as $info) {
    $relationship_id = $info['relationship_id'];
    $entity_type = $info['entity_type'];
    $entity_info = $entity_types[$entity_type];
    $id_key = !$info['revision'] ? $entity_info
      ->getKey('id') : $entity_info
      ->getKey('revision');
    $id_alias = $this
      ->getFieldAlias($info['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_type[$entity_type][$index][$relationship_id] = $result->{$id_alias};
      }
    }
  }

  // Load all entities and assign them to the correct result row.
  foreach ($ids_by_type as $entity_type => $ids) {
    $flat_ids = iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($ids)), FALSE);

    // Drupal core currently has no way to load multiple revisions. Sad.
    if (isset($entity_information[$entity_type]['revision']) && $entity_information[$entity_type]['revision'] === TRUE) {
      $entities = array();
      foreach ($flat_ids as $revision_id) {
        $entity = entity_revision_load($entity_type, $revision_id);
        if ($entity) {
          $entities[$revision_id] = $entity;
        }
      }
    }
    else {
      $entities = entity_load_multiple($entity_type, $flat_ids);
    }
    foreach ($ids as $index => $relationships) {
      foreach ($relationships as $relationship_id => $entity_id) {
        if (isset($entities[$entity_id])) {
          $entity = $entities[$entity_id];
        }
        else {
          $entity = NULL;
        }
        if ($relationship_id == 'none') {
          $results[$index]->_entity = $entity;
        }
        else {
          $results[$index]->_relationship_entities[$relationship_id] = $entity;
        }
      }
    }
  }
}