summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2015-02-05 09:16:52 (GMT)
committerAlex Pott2015-02-05 09:16:52 (GMT)
commitcf40fa1fbf7652bf4eebc3484028df9c4e872f3f (patch)
tree595bb468832e3c9866919caf350c3517424393e4
parent0509fbe78955acf7a8a6a44f0c0323337f2de43d (diff)
Issue #2300101 by dpi, ivanjaros: Unrevisioned base fields are ignored during entity load
-rw-r--r--core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php38
-rw-r--r--core/modules/serialization/src/Tests/EntitySerializationTest.php4
-rw-r--r--core/modules/system/src/Tests/Entity/EntityRevisionsTest.php6
-rw-r--r--core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php5
4 files changed, 38 insertions, 15 deletions
diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php
index a4e540d..be6ff7f 100644
--- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php
+++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php
@@ -665,31 +665,39 @@ class SqlContentEntityStorage extends ContentEntityStorageBase implements SqlEnt
// If a revision table is available, we need all the properties of the
// latest revision. Otherwise we fall back to the data table.
$table = $this->revisionDataTable ?: $this->dataTable;
- $query = $this->database->select($table, 'data', array('fetch' => \PDO::FETCH_ASSOC))
- ->fields('data')
- ->condition($this->idKey, array_keys($entities), 'IN')
- ->orderBy('data.' . $this->idKey);
+ $alias = $this->revisionDataTable ? 'revision' : 'data';
+ $query = $this->database->select($table, $alias, array('fetch' => \PDO::FETCH_ASSOC))
+ ->fields($alias)
+ ->condition($alias . '.' . $this->idKey, array_keys($entities), 'IN')
+ ->orderBy($alias . '.' . $this->idKey);
+ $table_mapping = $this->getTableMapping();
if ($this->revisionDataTable) {
+ // Find revisioned fields that are not entity keys.
+ $fields = array_diff($table_mapping->getFieldNames($this->revisionDataTable), $table_mapping->getFieldNames($this->baseTable));
+
+ // Find fields that are not revisioned or entity keys. Data fields have
+ // the same value regardless of entity revision.
+ $data_fields = array_diff($table_mapping->getFieldNames($this->dataTable), $fields, $table_mapping->getFieldNames($this->baseTable));
+ if ($data_fields) {
+ $fields = array_merge($fields, $data_fields);
+ $query->leftJoin($this->dataTable, 'data', "(revision.$this->idKey = data.$this->idKey)");
+ $query->fields('data', $data_fields);
+ }
+
// Get the revision IDs.
$revision_ids = array();
foreach ($entities as $values) {
$revision_ids[] = is_object($values) ? $values->getRevisionId() : $values[$this->revisionKey][LanguageInterface::LANGCODE_DEFAULT];
}
- $query->condition($this->revisionKey, $revision_ids, 'IN');
- }
-
- $data = $query->execute();
-
- $table_mapping = $this->getTableMapping();
- $translations = array();
- if ($this->revisionDataTable) {
- $data_fields = array_diff($table_mapping->getFieldNames($this->revisionDataTable), $table_mapping->getFieldNames($this->baseTable));
+ $query->condition('revision.' . $this->revisionKey, $revision_ids, 'IN');
}
else {
- $data_fields = $table_mapping->getFieldNames($this->dataTable);
+ $fields = $table_mapping->getFieldNames($this->dataTable);
}
+ $translations = array();
+ $data = $query->execute();
foreach ($data as $values) {
$id = $values[$this->idKey];
@@ -699,7 +707,7 @@ class SqlContentEntityStorage extends ContentEntityStorageBase implements SqlEnt
$translations[$id][$langcode] = TRUE;
- foreach ($data_fields as $field_name) {
+ foreach ($fields as $field_name) {
$columns = $table_mapping->getColumnNames($field_name);
// Do not key single-column fields by property name.
if (count($columns) == 1) {
diff --git a/core/modules/serialization/src/Tests/EntitySerializationTest.php b/core/modules/serialization/src/Tests/EntitySerializationTest.php
index d45c2a6..2a0bf9d 100644
--- a/core/modules/serialization/src/Tests/EntitySerializationTest.php
+++ b/core/modules/serialization/src/Tests/EntitySerializationTest.php
@@ -100,6 +100,9 @@ class EntitySerializationTest extends NormalizerTestBase {
'format' => $this->values['field_test_text']['format'],
),
),
+ 'created' => array(
+ array('value' => $this->entity->created->value),
+ ),
);
$normalized = $this->serializer->normalize($this->entity);
@@ -152,6 +155,7 @@ class EntitySerializationTest extends NormalizerTestBase {
'user_id' => '<user_id><target_id>' . $this->values['user_id'] . '</target_id></user_id>',
'revision_id' => '<revision_id><value>' . $this->entity->getRevisionId() . '</value></revision_id>',
'field_test_text' => '<field_test_text><value>' . $this->values['field_test_text']['value'] . '</value><format>' . $this->values['field_test_text']['format'] . '</format></field_test_text>',
+ 'created' => '<created><value>' . $this->entity->created->value . '</value></created>',
);
// Sort it in the same order as normalised.
$expected = array_merge($normalized, $expected);
diff --git a/core/modules/system/src/Tests/Entity/EntityRevisionsTest.php b/core/modules/system/src/Tests/Entity/EntityRevisionsTest.php
index d484c8b..06168a2 100644
--- a/core/modules/system/src/Tests/Entity/EntityRevisionsTest.php
+++ b/core/modules/system/src/Tests/Entity/EntityRevisionsTest.php
@@ -63,6 +63,7 @@ class EntityRevisionsTest extends WebTestBase {
$names = array();
$texts = array();
+ $created = array();
$revision_ids = array();
// Create three revisions.
@@ -76,6 +77,7 @@ class EntityRevisionsTest extends WebTestBase {
$entity->setNewRevision(TRUE);
$names[] = $entity->name->value = $this->randomMachineName(32);
$texts[] = $entity->field_test_text->value = $this->randomMachineName(32);
+ $created[] = $entity->created->value = time() + $i + 1;
$entity->save();
$revision_ids[] = $entity->revision_id->value;
@@ -93,6 +95,10 @@ class EntityRevisionsTest extends WebTestBase {
$this->assertEqual($entity_revision->revision_id->value, $revision_ids[$i], format_string('%entity_type: Revision ID matches.', array('%entity_type' => $entity_type)));
$this->assertEqual($entity_revision->name->value, $names[$i], format_string('%entity_type: Name matches.', array('%entity_type' => $entity_type)));
$this->assertEqual($entity_revision->field_test_text->value, $texts[$i], format_string('%entity_type: Text matches.', array('%entity_type' => $entity_type)));
+
+ // Check non-revisioned values are loaded.
+ $this->assertTrue(isset($entity_revision->created->value), format_string('%entity_type: Non-revisioned field is loaded.', array('%entity_type' => $entity_type)));
+ $this->assertEqual($entity_revision->created->value, $created[2], format_string('%entity_type: Non-revisioned field value is the same between revisions.', array('%entity_type' => $entity_type)));
}
// Confirm the correct revision text appears in the edit form.
diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php
index 0d6234d..0549257 100644
--- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php
+++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php
@@ -100,6 +100,11 @@ class EntityTest extends ContentEntityBase implements EntityOwnerInterface {
->setDescription(t('The bundle of the test entity.'))
->setRequired(TRUE);
+ $fields['created'] = BaseFieldDefinition::create('created')
+ ->setLabel(t('Authored on'))
+ ->setDescription(t('Time the entity was created'))
+ ->setTranslatable(TRUE);
+
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User ID'))
->setDescription(t('The ID of the associated user.'))