diff -u b/eck.install b/eck.install --- b/eck.install +++ b/eck.install @@ -52,6 +52,9 @@ $config->set('revisionable', TRUE); $config->save(); } + + \Drupal::entityTypeManager()->clearCachedDefinitions(); + \Drupal::entityTypeManager()->getStorage('eck_entity_type')->resetCache(); } /** diff -u b/eck.module b/eck.module --- b/eck.module +++ b/eck.module @@ -5,6 +5,7 @@ * Contains hook implementations. */ +use Drupal\Component\Utility\NestedArray; use Drupal\Core\Config\Entity\ConfigEntityType; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\Entity; @@ -136,7 +137,7 @@ } // Merge the definitions. - $definition = array_merge($definition, $base_definition); + $definition = NestedArray::mergeDeep($definition, $base_definition); // Add the new content entity to the entity types. $entity_types[$definition['id']] = new ContentEntityType($definition); diff -u b/src/Entity/RevisionableSchemaConverter.php b/src/Entity/RevisionableSchemaConverter.php --- b/src/Entity/RevisionableSchemaConverter.php +++ b/src/Entity/RevisionableSchemaConverter.php @@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Entity\Sql\DefaultTableMapping; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\StringTranslation\TranslatableMarkup; @@ -88,8 +89,8 @@ ]; $options = array_merge($default_options, $options); $this->updateEntityType($entity_type_id, $options); - $this->installRevisionableFields($entity_type_id, $options); $this->createTables($entity_type_id, $options); + $this->installRevisionableFields($entity_type_id, $options); } /** @@ -107,22 +108,29 @@ ->fetchField(); } - /** @var \Drupal\Core\Entity\Sql\TableMappingInterface $table_mapping */ + /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ $table_mapping = $this->entityTypeManager->getStorage($entity_type->id())->getTableMapping(); $table_names = $table_mapping->getTableNames(); $id = $entity_type->getKey('id'); + debug($entity_type); + debug($id); $revision_id = $entity_type->getKey('revision'); $data = []; // Loop through all tables for the entity type and combine the data. foreach ($table_names as $table_name) { + $id_column = $id; + if ($this->requiredDedicatedTable($table_name, $table_mapping)) { + $id_column = 'entity_id'; + } + $column_names = $table_mapping->getAllColumns($table_name); // Process 5 entities per batch. $results = $this->database->select($table_name, 't') ->fields('t') - ->condition($id, $sandbox['current_id'], '>') + ->condition($id_column, $sandbox['current_id'], '>') ->range(0, 5) - ->orderBy($id) + ->orderBy($id_column) ->execute() ->fetchAll(); foreach ($results as $key => $result) { @@ -147,6 +155,10 @@ $values[$column_name] = $record[$column_name]; } } + if ($this->requiredDedicatedTable($table_name, $table_mapping)) { + $values['delta'] = 0; + } + $this->database->upsert($table_name)->key('id')->fields($values)->execute(); } $sandbox['progress']++; @@ -155,6 +167,10 @@ $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']); } + protected function requiredDedicatedTable($table_name, DefaultTableMapping $table_mapping) { + return in_array($table_name, $table_mapping->getDedicatedTableNames(), TRUE); + } + /** * Updates installed entity type definition. * diff -u b/src/Tests/RevisionUpdatePathTest.php b/src/Tests/RevisionUpdatePathTest.php --- b/src/Tests/RevisionUpdatePathTest.php +++ b/src/Tests/RevisionUpdatePathTest.php @@ -28,11 +28,26 @@ public function testUpdatePath() { $this->runUpdates(); + /** @var \Drupal\eck\Entity\EckEntity $entity */ $storage = \Drupal::entityTypeManager()->getStorage('entity_type_with_revision'); $entity = $storage->load(1); $this->assertEqual($entity->field_test_field->value, 'test field value 1'); + $this->assertEqual($entity->title->value, 'test content 1'); $entity = $storage->load(2); - $this->assertEqual($entity->field_test_field->value, 'test field value '); + $this->assertEqual($entity->field_test_field->value, 'test field value 2'); + $this->assertEqual($entity->title->value, 'test content 2'); + + // Ensure that revision support works. + $entity->setNewRevision(TRUE); + $entity->isDefaultRevision(FALSE); + $entity->field_test_field->value = 'test field value 2.0'; + $entity->save(); + $revision_id = $entity->getRevisionId(); + + $entity = $storage->load(2); + $this->assertEqual($entity->field_test_field->value, 'test field value 2'); + $revision = $storage->loadRevision($revision_id); + $this->assertEqual($revision->field_test_field->value, 'test field value 2.0'); } }