Issue #2300101 by dpi, ivanjaros: Unrevisioned base fields are ignored during entity load
parent
0509fbe789
commit
cf40fa1fbf
|
@ -665,31 +665,39 @@ class SqlContentEntityStorage extends ContentEntityStorageBase implements SqlEnt
|
||||||
// If a revision table is available, we need all the properties of the
|
// If a revision table is available, we need all the properties of the
|
||||||
// latest revision. Otherwise we fall back to the data table.
|
// latest revision. Otherwise we fall back to the data table.
|
||||||
$table = $this->revisionDataTable ?: $this->dataTable;
|
$table = $this->revisionDataTable ?: $this->dataTable;
|
||||||
$query = $this->database->select($table, 'data', array('fetch' => \PDO::FETCH_ASSOC))
|
$alias = $this->revisionDataTable ? 'revision' : 'data';
|
||||||
->fields('data')
|
$query = $this->database->select($table, $alias, array('fetch' => \PDO::FETCH_ASSOC))
|
||||||
->condition($this->idKey, array_keys($entities), 'IN')
|
->fields($alias)
|
||||||
->orderBy('data.' . $this->idKey);
|
->condition($alias . '.' . $this->idKey, array_keys($entities), 'IN')
|
||||||
|
->orderBy($alias . '.' . $this->idKey);
|
||||||
|
|
||||||
|
$table_mapping = $this->getTableMapping();
|
||||||
if ($this->revisionDataTable) {
|
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.
|
// Get the revision IDs.
|
||||||
$revision_ids = array();
|
$revision_ids = array();
|
||||||
foreach ($entities as $values) {
|
foreach ($entities as $values) {
|
||||||
$revision_ids[] = is_object($values) ? $values->getRevisionId() : $values[$this->revisionKey][LanguageInterface::LANGCODE_DEFAULT];
|
$revision_ids[] = is_object($values) ? $values->getRevisionId() : $values[$this->revisionKey][LanguageInterface::LANGCODE_DEFAULT];
|
||||||
}
|
}
|
||||||
$query->condition($this->revisionKey, $revision_ids, 'IN');
|
$query->condition('revision.' . $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));
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$data_fields = $table_mapping->getFieldNames($this->dataTable);
|
$fields = $table_mapping->getFieldNames($this->dataTable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$translations = array();
|
||||||
|
$data = $query->execute();
|
||||||
foreach ($data as $values) {
|
foreach ($data as $values) {
|
||||||
$id = $values[$this->idKey];
|
$id = $values[$this->idKey];
|
||||||
|
|
||||||
|
@ -699,7 +707,7 @@ class SqlContentEntityStorage extends ContentEntityStorageBase implements SqlEnt
|
||||||
$translations[$id][$langcode] = TRUE;
|
$translations[$id][$langcode] = TRUE;
|
||||||
|
|
||||||
|
|
||||||
foreach ($data_fields as $field_name) {
|
foreach ($fields as $field_name) {
|
||||||
$columns = $table_mapping->getColumnNames($field_name);
|
$columns = $table_mapping->getColumnNames($field_name);
|
||||||
// Do not key single-column fields by property name.
|
// Do not key single-column fields by property name.
|
||||||
if (count($columns) == 1) {
|
if (count($columns) == 1) {
|
||||||
|
|
|
@ -100,6 +100,9 @@ class EntitySerializationTest extends NormalizerTestBase {
|
||||||
'format' => $this->values['field_test_text']['format'],
|
'format' => $this->values['field_test_text']['format'],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
'created' => array(
|
||||||
|
array('value' => $this->entity->created->value),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
$normalized = $this->serializer->normalize($this->entity);
|
$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>',
|
'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>',
|
'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>',
|
'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.
|
// Sort it in the same order as normalised.
|
||||||
$expected = array_merge($normalized, $expected);
|
$expected = array_merge($normalized, $expected);
|
||||||
|
|
|
@ -63,6 +63,7 @@ class EntityRevisionsTest extends WebTestBase {
|
||||||
|
|
||||||
$names = array();
|
$names = array();
|
||||||
$texts = array();
|
$texts = array();
|
||||||
|
$created = array();
|
||||||
$revision_ids = array();
|
$revision_ids = array();
|
||||||
|
|
||||||
// Create three revisions.
|
// Create three revisions.
|
||||||
|
@ -76,6 +77,7 @@ class EntityRevisionsTest extends WebTestBase {
|
||||||
$entity->setNewRevision(TRUE);
|
$entity->setNewRevision(TRUE);
|
||||||
$names[] = $entity->name->value = $this->randomMachineName(32);
|
$names[] = $entity->name->value = $this->randomMachineName(32);
|
||||||
$texts[] = $entity->field_test_text->value = $this->randomMachineName(32);
|
$texts[] = $entity->field_test_text->value = $this->randomMachineName(32);
|
||||||
|
$created[] = $entity->created->value = time() + $i + 1;
|
||||||
$entity->save();
|
$entity->save();
|
||||||
$revision_ids[] = $entity->revision_id->value;
|
$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->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->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)));
|
$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.
|
// Confirm the correct revision text appears in the edit form.
|
||||||
|
|
|
@ -100,6 +100,11 @@ class EntityTest extends ContentEntityBase implements EntityOwnerInterface {
|
||||||
->setDescription(t('The bundle of the test entity.'))
|
->setDescription(t('The bundle of the test entity.'))
|
||||||
->setRequired(TRUE);
|
->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')
|
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
|
||||||
->setLabel(t('User ID'))
|
->setLabel(t('User ID'))
|
||||||
->setDescription(t('The ID of the associated user.'))
|
->setDescription(t('The ID of the associated user.'))
|
||||||
|
|
Loading…
Reference in New Issue