Commit cf40fa1f authored by alexpott's avatar alexpott

Issue #2300101 by dpi, ivanjaros: Unrevisioned base fields are ignored during entity load

parent 0509fbe7
......@@ -665,31 +665,39 @@ protected function attachPropertyData(array &$entities) {
// 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 @@ protected function attachPropertyData(array &$entities) {
$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) {
......
......@@ -100,6 +100,9 @@ public function testNormalize() {
'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 @@ public function testSerialize() {
'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);
......
......@@ -63,6 +63,7 @@ protected function assertRevisions($entity_type) {
$names = array();
$texts = array();
$created = array();
$revision_ids = array();
// Create three revisions.
......@@ -76,6 +77,7 @@ protected function assertRevisions($entity_type) {
$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 @@ protected function assertRevisions($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->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.
......
......@@ -100,6 +100,11 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->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.'))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment