diff --git a/core/lib/Drupal/Core/Entity/RevisionableSchemaConverter.php b/core/lib/Drupal/Core/Entity/RevisionableSchemaConverter.php index 4d90601..dafd8db 100644 --- a/core/lib/Drupal/Core/Entity/RevisionableSchemaConverter.php +++ b/core/lib/Drupal/Core/Entity/RevisionableSchemaConverter.php @@ -10,26 +10,31 @@ class RevisionableSchemaConverter implements RevisionableSchemaConverterInterfac /** * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface + * Last installed schemea repository service. */ protected $lastInstalledSchemaRepository; /** * @var \Drupal\Core\Entity\EntityTypeManagerInterface + * Entity Type Manager service. */ protected $entityTypeManager; /** * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface + * Entity definition update manager service. */ protected $entityDefinitionUpdateManager; /** * @var \Drupal\Core\Entity\EntityFieldManagerInterface + * Entity field manager service. */ protected $entityFieldManager; /** * @var \Drupal\Core\Database\Connection + * Database connection. */ protected $database; @@ -37,10 +42,15 @@ class RevisionableSchemaConverter implements RevisionableSchemaConverterInterfac * EntitySchemaUpdater constructor. * * @param \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $last_installed_schema_repository + * Last installed schema repository service. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * Entity type manager service. * @param \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface $entity_definition_update_manager + * Entity definition update manager service. * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager + * Entity field manager service. * @param \Drupal\Core\Database\Connection $database + * Database connection. */ public function __construct(EntityLastInstalledSchemaRepositoryInterface $last_installed_schema_repository, EntityTypeManagerInterface $entity_type_manager, EntityDefinitionUpdateManagerInterface $entity_definition_update_manager, EntityFieldManagerInterface $entity_field_manager, Connection $database) { $this->lastInstalledSchemaRepository = $last_installed_schema_repository; @@ -53,10 +63,10 @@ public function __construct(EntityLastInstalledSchemaRepositoryInterface $last_i /** * {@inheritdoc} */ - public function convertSchema(EntityTypeInterface $entity_type) { - $this->updateEntityType($entity_type); - $this->createTables($entity_type); - $this->installRevisionableFields($entity_type); + public function convertSchema($entity_type_id, $options) { + $this->updateEntityType($entity_type_id, $options); + $this->createTables($entity_type_id); + $this->installRevisionableFields($entity_type_id, $options); } /** @@ -126,14 +136,15 @@ public function copyData(EntityTypeInterface $entity_type, array &$sandbox) { * Updates installed entity type definition. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * Entity type to be updated. */ - protected function updateEntityType(EntityTypeInterface $entity_type) { - $last_entity_type = $this->lastInstalledSchemaRepository->getLastInstalledDefinition($entity_type->id()); + protected function updateEntityType($entity_type_id, $options) { + $last_entity_type = $this->lastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id); $keys = $last_entity_type->getKeys(); - $keys['revision'] = $entity_type->getKey('revision'); + $keys['revision'] = $options['revision']; $last_entity_type->set('entity_keys', $keys); - $last_entity_type->set('revision_table', $entity_type->getRevisionTable()); - $last_entity_type->set('revision_data_table', $entity_type->getRevisionDataTable()); + $last_entity_type->set('revision_table', $options['revision_table']); + $last_entity_type->set('revision_data_table', $options['revision_data']); $this->lastInstalledSchemaRepository->setLastInstalledDefinition($last_entity_type); } @@ -141,10 +152,12 @@ protected function updateEntityType(EntityTypeInterface $entity_type) { * Creates missing tables. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * Entity type to be updated. */ - protected function createTables(EntityTypeInterface $entity_type) { + protected function createTables($entity_type_id) { /** @var \Drupal\Core\Entity\EntityTypeListenerInterface $storage */ - $storage = $this->entityTypeManager->getStorage($entity_type->id()); + $storage = $this->entityTypeManager->getStorage($entity_type_id); + $entity_type = $storage->getEntityType(); if ($storage instanceof EntityTypeListenerInterface) { $storage->onEntityTypeCreate($entity_type); } @@ -154,21 +167,22 @@ protected function createTables(EntityTypeInterface $entity_type) { * Installs new fields. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * Entity type to be updated. */ - protected function installRevisionableFields(EntityTypeInterface $entity_type) { + protected function installRevisionableFields($entity_type_id, $options) { $revision_id = BaseFieldDefinition::create('integer') ->setLabel(new TranslatableMarkup('Revision ID')) ->setReadOnly(TRUE) ->setSetting('unsigned', TRUE); $this->entityDefinitionUpdateManager - ->installFieldStorageDefinition($entity_type->getKey('revision'), $entity_type->id(), $entity_type->id(), $revision_id); + ->installFieldStorageDefinition($options['revision'], $entity_type_id, $entity_type_id, $revision_id); $revision_created = BaseFieldDefinition::create('created') ->setLabel(t('Revision create time')) ->setDescription(t('The time that the current revision was created.')) ->setRevisionable(TRUE); $this->entityDefinitionUpdateManager - ->installFieldStorageDefinition("revision_created", $entity_type->id(), $entity_type->id(), $revision_created); + ->installFieldStorageDefinition("revision_created", $entity_type_id, $entity_type_id, $revision_created); $revision_user = BaseFieldDefinition::create('entity_reference') ->setLabel(t('Revision user')) @@ -176,7 +190,7 @@ protected function installRevisionableFields(EntityTypeInterface $entity_type) { ->setSetting('target_type', 'user') ->setRevisionable(TRUE); $this->entityDefinitionUpdateManager - ->installFieldStorageDefinition("revision_user", $entity_type->id(), $entity_type->id(), $revision_user); + ->installFieldStorageDefinition("revision_user", $entity_type_id, $entity_type_id, $revision_user); $revision_log_message = BaseFieldDefinition::create('string_long') ->setLabel(t('Revision log message')) @@ -191,18 +205,17 @@ protected function installRevisionableFields(EntityTypeInterface $entity_type) { ], ]); $this->entityDefinitionUpdateManager - ->installFieldStorageDefinition("revision_log_message", $entity_type->id(), $entity_type->id(), $revision_log_message); + ->installFieldStorageDefinition("revision_log_message", $entity_type_id, $entity_type_id, $revision_log_message); /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $field_definitions */ - $field_definitions = $this->entityFieldManager->getBaseFieldDefinitions($entity_type->id()); + $field_definitions = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id); foreach ($field_definitions as $field_definition) { $this->entityDefinitionUpdateManager->updateFieldStorageDefinition($field_definition); } - $revision_data_table = $entity_type->getRevisionDataTable(); $schema = $this->database->schema(); - $schema->dropPrimaryKey($revision_data_table); - $schema->addPrimaryKey($revision_data_table, [$entity_type->getKey('revision'), $entity_type->getKey('langcode')]); + $schema->dropPrimaryKey($options['revision_data_table']); + $schema->addPrimaryKey($options['revision_data_table'], [$options['revision'], 'langcode']); } } diff --git a/core/lib/Drupal/Core/Entity/RevisionableSchemaConverterInterface.php b/core/lib/Drupal/Core/Entity/RevisionableSchemaConverterInterface.php index 41d7bab..e1a90fd 100644 --- a/core/lib/Drupal/Core/Entity/RevisionableSchemaConverterInterface.php +++ b/core/lib/Drupal/Core/Entity/RevisionableSchemaConverterInterface.php @@ -10,7 +10,7 @@ * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type to convert. */ - public function convertSchema(EntityTypeInterface $entity_type); + public function convertSchema($entity_type_id, $options); /** * Copies existing data to new tables. diff --git a/core/modules/shortcut/shortcut.install b/core/modules/shortcut/shortcut.install index b902170..8c3f444 100644 --- a/core/modules/shortcut/shortcut.install +++ b/core/modules/shortcut/shortcut.install @@ -73,7 +73,11 @@ function shortcut_uninstall() { */ function shortcut_update_8001() { $entity_type = \Drupal::entityTypeManager()->getStorage('shortcut')->getEntityType(); - \Drupal::service('entity.revisionable_schema_converter')->convertSchema($entity_type); + \Drupal::service('entity.revisionable_schema_converter')->convertSchema('shortcut', [ + 'revision' => 'revision_id', + 'revision_table' => 'shortcut_revision', + 'revision_data_table' => 'shortcut_field_revision', + ]); } /** diff --git a/core/tests/Drupal/Tests/Core/Entity/RevisionableSchemaConverterTest.php b/core/tests/Drupal/Tests/Core/Entity/RevisionableSchemaConverterTest.php index 4749783..617fbb1 100644 --- a/core/tests/Drupal/Tests/Core/Entity/RevisionableSchemaConverterTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/RevisionableSchemaConverterTest.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\Core\Entity; use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\shortcut\Entity\Shortcut; use Drupal\system\Tests\Update\UpdatePathTestBase; /** @@ -16,35 +17,35 @@ class RevisionableSchemaConverterTest extends UpdatePathTestBase { /** - * @inheritDoc + * {@inheritdoc} */ protected function setDatabaseDumpFiles() { $this->databaseDumpFiles = [__DIR__ . '/../../../../../modules/system/tests/fixtures/update/drupal-8.filled.standard.php.gz']; } /** - * Runs the updates then test the entity type is revisionable and all - * shortcut entities are accessible. + * Tests the entity type is revisionable and shortcut entities are accessible. */ public function testMakeRevisionable() { - $entity_type = \Drupal::entityTypeManager()->getStorage('shortcut')->getEntityType(); - - /** @var ContentEntityInterface $last_installed */ - $pre_last_installed = \Drupal::service('entity.last_installed_schema.repository')->getLastInstalledDefinition($entity_type->id()); - $this->assertFalse($pre_last_installed->isRevisionable()); - $this->runUpdates(); - /** @var ContentEntityInterface $last_installed */ - $post_last_installed = \Drupal::service('entity.last_installed_schema.repository')->getLastInstalledDefinition($entity_type->id()); - $this->assertTrue($post_last_installed->isRevisionable()); + $entity_type_manager = $this->container->get('entity_type.manager'); + $shortcut = $entity_type_manager->getStorage('shortcut')->getEntityType(); - $storage = \Drupal::entityTypeManager()->getStorage($entity_type->id()); - $entities = $storage->loadMultiple(); - $this->assertEqual(count($entities), 4, "Four Shortcut entities found"); + /** @var ContentEntityInterface $last_installed_shortcut */ + $last_installed_shortcut = $this->container->get('entity.last_installed_schema.repository')->getLastInstalledDefinition($shortcut->id()); + $this->assertTrue($last_installed_shortcut->isRevisionable()); - $revision_one = $storage->loadRevision(1); + $shortcut_storage = $entity_type_manager->getStorage($shortcut->id()); + $shortcut_entities = $shortcut_storage->loadMultiple(); + $this->assertEqual(count($shortcut_entities), 4, "Four Shortcut entities found"); + + $revision_one = $shortcut_storage->loadRevision(1); $this->assertEqual(1, $revision_one->getRevisionId(), "Revision 1 is revision 1"); + + $new_shortcut = Shortcut::create(['shortcut_set' => 'default']); + $new_shortcut->save(); + $this->assertEqual(5, $new_shortcut->id(), "New shortcut created"); } }