diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php index c83b72f..5a64069 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php @@ -477,7 +477,7 @@ protected function doLoadMutlipleRevisions(array $revision_ids) { // Map the loaded records into entity objects and according fields. if ($records) { - $revisions = $this->mapFromStorageRecords($records, TRUE); + $revisions = $this->mapFromStorageRecords($records, TRUE, 'revision_id'); } } @@ -488,14 +488,17 @@ protected function doLoadMutlipleRevisions(array $revision_ids) { * Maps from storage records to entity objects, and attaches fields. * * @param array $records - * Associative array of query results, keyed on the entity ID. + * Associative array of query results, keyed on the entity ID or revision + * ID. * @param bool $load_from_revision * Flag to indicate whether revisions should be loaded or not. + * @param string $record_key + * How $records is keyed. Either 'entity_id' or 'revision_id'. * * @return array * An array of entity objects implementing the EntityInterface. */ - protected function mapFromStorageRecords(array $records, $load_from_revision = FALSE) { + protected function mapFromStorageRecords(array $records, $load_from_revision = FALSE, $record_key = 'entity_id') { if (!$records) { return array(); } @@ -525,8 +528,8 @@ protected function mapFromStorageRecords(array $records, $load_from_revision = F $translations = array_fill_keys(array_keys($values), array()); // Load values from shared and dedicated tables. - $this->loadFromSharedTables($values, $translations); - $this->loadFromDedicatedTables($values, $load_from_revision); + $this->loadFromSharedTables($values, $translations, $record_key); + $this->loadFromDedicatedTables($values, $load_from_revision, $record_key); $entities = array(); foreach ($values as $id => $entity_values) { @@ -542,11 +545,20 @@ protected function mapFromStorageRecords(array $records, $load_from_revision = F * Loads values for fields stored in the shared data tables. * * @param array &$values - * Associative array of entities values, keyed on the entity ID. + * Associative array of entities values, keyed on the entity ID or revision + * ID. * @param array &$translations * List of translations, keyed on the entity ID. + * @param string $record_key + * How $values is keyed. Either 'entity_id' or 'revision_id'. */ - protected function loadFromSharedTables(array &$values, array &$translations) { + protected function loadFromSharedTables(array &$values, array &$translations, $record_key = 'entity_id') { + if ($record_key === 'revision_id') { + $record_key = $this->revisionKey; + } + else { + $record_key = $this->idKey; + } if ($this->dataTable) { // If a revision table is available, we need all the properties of the // latest revision. Otherwise we fall back to the data table. @@ -554,8 +566,8 @@ protected function loadFromSharedTables(array &$values, array &$translations) { $alias = $this->revisionDataTable ? 'revision' : 'data'; $query = $this->database->select($table, $alias, array('fetch' => \PDO::FETCH_ASSOC)) ->fields($alias) - ->condition($alias . '.' . $this->idKey, array_keys($values), 'IN') - ->orderBy($alias . '.' . $this->idKey); + ->condition($alias . '.' . $record_key, array_keys($values), 'IN') + ->orderBy($alias . '.' . $record_key); $table_mapping = $this->getTableMapping(); if ($this->revisionDataTable) { @@ -600,7 +612,7 @@ protected function loadFromSharedTables(array &$values, array &$translations) { $result = $query->execute(); foreach ($result as $row) { - $id = $row[$this->idKey]; + $id = $row[$record_key]; // Field values in default language are stored with // LanguageInterface::LANGCODE_DEFAULT as key. @@ -1133,12 +1145,14 @@ protected function getQueryServiceName() { * Loads values of fields stored in dedicated tables for a group of entities. * * @param array &$values - * An array of values keyed by entity ID. + * An array of values keyed by entity ID or revision ID. * @param bool $load_from_revision * (optional) Flag to indicate whether revisions should be loaded or not, * defaults to FALSE. + * @param string $record_key + * How $records is keyed. Either 'entity_id' or 'revision_id'. */ - protected function loadFromDedicatedTables(array &$values, $load_from_revision) { + protected function loadFromDedicatedTables(array &$values, $load_from_revision, $record_key = 'entity_id') { if (empty($values)) { return; } @@ -1188,7 +1202,7 @@ protected function loadFromDedicatedTables(array &$values, $load_from_revision) foreach ($results as $row) { $bundle = $row->bundle; - $value_key = $load_from_revision ? $row->revision_id : $row->entity_id; + $value_key = $row->$record_key; // Field values in default language are stored with // LanguageInterface::LANGCODE_DEFAULT as key. $langcode = LanguageInterface::LANGCODE_DEFAULT;