diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php b/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php index 107495b..6caf20b 100644 --- a/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php +++ b/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php @@ -413,7 +413,10 @@ public function addField($table, $field, $spec, $keys_new = array()) { } if ($fixnull) { $spec['not null'] = TRUE; - $this->changeField($table, $field, $field, $spec); + $count = $this->connection->query('SELECT COUNT(*) AS count FROM {' . $table . '}')->fetchAssoc(); + if ($count['count'] == 0) { + $this->changeField($table, $field, $field, $spec); + }; } } diff --git a/core/modules/shortcut/shortcut.install b/core/modules/shortcut/shortcut.install index 8df408f..32d2321 100644 --- a/core/modules/shortcut/shortcut.install +++ b/core/modules/shortcut/shortcut.install @@ -67,3 +67,13 @@ function shortcut_uninstall() { \Drupal::configFactory()->getEditable('seven.settings')->clear('third_party_settings.shortcut')->save(TRUE); } } + +/** + * Update schema. + */ +function shortcut_update_8001() { + $entity_type = \Drupal::entityTypeManager()->getStorage('shortcut')->getEntityType(); + \Drupal::service('system.entity_schema_updater')->createTables($entity_type); + \Drupal::service('system.entity_schema_updater')->installFields($entity_type); + \Drupal::service('system.entity_schema_updater')->updateEntityType($entity_type); +} diff --git a/core/modules/shortcut/src/Entity/Shortcut.php b/core/modules/shortcut/src/Entity/Shortcut.php index 6ddaf05..704e160 100644 --- a/core/modules/shortcut/src/Entity/Shortcut.php +++ b/core/modules/shortcut/src/Entity/Shortcut.php @@ -3,9 +3,9 @@ namespace Drupal\shortcut\Entity; use Drupal\Core\Cache\Cache; -use Drupal\Core\Entity\ContentEntityBase; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Entity\RevisionableContentEntityBase; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\link\LinkItemInterface; use Drupal\shortcut\ShortcutInterface; @@ -33,6 +33,7 @@ * translatable = TRUE, * entity_keys = { * "id" = "id", + * "revision" = "revision_id", * "uuid" = "uuid", * "bundle" = "shortcut_set", * "label" = "title", @@ -47,7 +48,7 @@ * bundle_entity_type = "shortcut_set" * ) */ -class Shortcut extends ContentEntityBase implements ShortcutInterface { +class Shortcut extends RevisionableContentEntityBase implements ShortcutInterface { /** * {@inheritdoc} @@ -122,6 +123,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setDescription(t('The name of the shortcut.')) ->setRequired(TRUE) ->setTranslatable(TRUE) + ->setRevisionable(TRUE) ->setSetting('max_length', 255) ->setDisplayOptions('form', array( 'type' => 'string_textfield', @@ -139,6 +141,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setLabel(t('Path')) ->setDescription(t('The location this shortcut points to.')) ->setRequired(TRUE) + ->setRevisionable(TRUE) ->setSettings(array( 'link_type' => LinkItemInterface::LINK_INTERNAL, 'title' => DRUPAL_DISABLED, diff --git a/core/modules/system/src/EntitySchemaUpdater.php b/core/modules/system/src/EntitySchemaUpdater.php index d83a3fc..6521036 100644 --- a/core/modules/system/src/EntitySchemaUpdater.php +++ b/core/modules/system/src/EntitySchemaUpdater.php @@ -23,7 +23,7 @@ public function createTables(EntityTypeInterface $entity_type) { } } - public function addRevisionField(EntityTypeInterface $entity_type) { + public function installFields(EntityTypeInterface $entity_type) { if ($entity_type->hasKey('revision')) { $revision_id = BaseFieldDefinition::create('integer') ->setLabel(new TranslatableMarkup('Revision ID')) @@ -33,39 +33,51 @@ public function addRevisionField(EntityTypeInterface $entity_type) { ->installFieldStorageDefinition($entity_type->getKey('revision'), $entity_type->id(), $entity_type->id(), $revision_id); } - if ($entity_type instanceof RevisionLogInterface) { - $revision_created = BaseFieldDefinition::create('created') - ->setLabel(t('Revision create time')) - ->setDescription(t('The time that the current revision was created.')) - ->setRevisionable(TRUE); - \Drupal::entityDefinitionUpdateManager() - ->installFieldStorageDefinition("revision_created", $entity_type->id(), $entity_type->id(), $revision_created); + $revision_created = BaseFieldDefinition::create('created') + ->setLabel(t('Revision create time')) + ->setDescription(t('The time that the current revision was created.')) + ->setRevisionable(TRUE); + \Drupal::entityDefinitionUpdateManager() + ->installFieldStorageDefinition("revision_created", $entity_type->id(), $entity_type->id(), $revision_created); - $revision_user = BaseFieldDefinition::create('entity_reference') - ->setLabel(t('Revision user')) - ->setDescription(t('The user ID of the author of the current revision.')) - ->setSetting('target_type', 'user') - ->setRevisionable(TRUE); - \Drupal::entityDefinitionUpdateManager() - ->installFieldStorageDefinition("revision_user", $entity_type->id(), $entity_type->id(), $revision_user); + $revision_user = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Revision user')) + ->setDescription(t('The user ID of the author of the current revision.')) + ->setSetting('target_type', 'user') + ->setRevisionable(TRUE); + \Drupal::entityDefinitionUpdateManager() + ->installFieldStorageDefinition("revision_user", $entity_type->id(), $entity_type->id(), $revision_user); - $revision_log_message = BaseFieldDefinition::create('string_long') - ->setLabel(t('Revision log message')) - ->setDescription(t('Briefly describe the changes you have made.')) - ->setRevisionable(TRUE) - ->setDefaultValue('') - ->setDisplayOptions('form', [ - 'type' => 'string_textarea', - 'weight' => 25, - 'settings' => [ - 'rows' => 4, - ], - ]); - \Drupal::entityDefinitionUpdateManager() - ->installFieldStorageDefinition("revision_log_message", $entity_type->id(), $entity_type->id(), $revision_log_message); + $revision_log_message = BaseFieldDefinition::create('string_long') + ->setLabel(t('Revision log message')) + ->setDescription(t('Briefly describe the changes you have made.')) + ->setRevisionable(TRUE) + ->setDefaultValue('') + ->setDisplayOptions('form', [ + 'type' => 'string_textarea', + 'weight' => 25, + 'settings' => [ + 'rows' => 4, + ], + ]); + \Drupal::entityDefinitionUpdateManager() + ->installFieldStorageDefinition("revision_log_message", $entity_type->id(), $entity_type->id(), $revision_log_message); + + /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $field_definitions */ + $field_definitions = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions($entity_type->id()); + foreach ($field_definitions as $field_definition) { + \Drupal::entityDefinitionUpdateManager()->updateFieldStorageDefinition($field_definition); } + } - \Drupal::service('entity.last_installed_schema.repository')->setLastInstalledDefinition($entity_type); + public function updateEntityType(EntityTypeInterface $entity_type) { + /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $last_installed_schema_repository */ + $last_installed_schema_repository = \Drupal::service('entity.last_installed_schema.repository'); + $last_entity_type = $last_installed_schema_repository->getLastInstalledDefinition($entity_type->id()); + $keys = $last_entity_type->getKeys(); + $keys['revision'] = 'revision_id'; + $last_entity_type->set('entity_keys', $keys); + $last_installed_schema_repository->setLastInstalledDefinition($last_entity_type); } } diff --git a/core/modules/system/src/EntitySchemaUpdaterInterface.php b/core/modules/system/src/EntitySchemaUpdaterInterface.php index 65f7c23..af6c6aa 100644 --- a/core/modules/system/src/EntitySchemaUpdaterInterface.php +++ b/core/modules/system/src/EntitySchemaUpdaterInterface.php @@ -14,6 +14,11 @@ public function createTables(EntityTypeInterface $entity_type); /** * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type */ - public function addRevisionField(EntityTypeInterface $entity_type); + public function installFields(EntityTypeInterface $entity_type); + + /** + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + */ + public function updateEntityType(EntityTypeInterface $entity_type); } diff --git a/core/modules/system/src/Tests/Update/EntitySchemaUpdaterTest.php b/core/modules/system/src/Tests/Update/EntitySchemaUpdaterTest.php index 4fc6f05..eb5b337 100644 --- a/core/modules/system/src/Tests/Update/EntitySchemaUpdaterTest.php +++ b/core/modules/system/src/Tests/Update/EntitySchemaUpdaterTest.php @@ -4,18 +4,21 @@ use Drupal\Core\Entity\ContentEntityInterface; +/** + * Class EntitySchemaUpdaterTest + * @package Drupal\system\Tests\Update + * @group entity + */ class EntitySchemaUpdaterTest extends UpdatePathTestBase { /** * @inheritDoc */ protected function setDatabaseDumpFiles() { - $this->databaseDumpFiles = [ - __DIR__ . '/../../../../system/tests/modules/entity_test/tests/fixtures/update/drupal-8.standard.non-rev-entity_test_mulrev.php.gz' - ]; + $this->databaseDumpFiles = [__DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz']; } public function testMakeRevisionable() { - $entity_type = \Drupal::entityTypeManager()->getStorage('entity_test_mulrev')->getEntityType(); + $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());