You are here

public function Sql::loadEntities in Drupal 9

Same name and namespace in other branches
  1. 8 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 1569

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 = [];
  foreach ($entity_information as $info) {
    $entity_type = $info['entity_type'];
    if (!isset($entity_types[$entity_type])) {
      $entity_types[$entity_type] = $this->entityTypeManager
        ->getDefinition($entity_type);
    }
  }

  // Assemble a list of entities to load.
  $entity_ids_by_type = [];
  $revision_ids_by_type = [];
  foreach ($entity_information as $info) {
    $relationship_id = $info['relationship_id'];
    $entity_type = $info['entity_type'];

    /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_info */
    $entity_info = $entity_types[$entity_type];
    $revision = $info['revision'];
    $id_key = !$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} != '') {
        if ($revision) {
          $revision_ids_by_type[$entity_type][$index][$relationship_id] = $result->{$id_alias};
        }
        else {
          $entity_ids_by_type[$entity_type][$index][$relationship_id] = $result->{$id_alias};
        }
      }
    }
  }

  // Load all entities and assign them to the correct result row.
  foreach ($entity_ids_by_type as $entity_type => $ids) {
    $entity_storage = $this->entityTypeManager
      ->getStorage($entity_type);
    $flat_ids = iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($ids)), FALSE);
    $entities = $entity_storage
      ->loadMultiple(array_unique($flat_ids));
    $results = $this
      ->assignEntitiesToResult($ids, $entities, $results);
  }

  // Now load all revisions.
  foreach ($revision_ids_by_type as $entity_type => $revision_ids) {
    $entity_storage = $this->entityTypeManager
      ->getStorage($entity_type);
    $entities = [];
    foreach ($revision_ids as $index => $revision_id_by_relationship) {
      foreach ($revision_id_by_relationship as $revision => $revision_id) {

        // Drupal core currently has no way to load multiple revisions.
        $entity = $entity_storage
          ->loadRevision($revision_id);
        $entities[$revision_id] = $entity;
      }
    }
    $results = $this
      ->assignEntitiesToResult($revision_ids, $entities, $results);
  }
}