diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 1f99e5f..fa78afe 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -1151,6 +1151,11 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setReadOnly(TRUE) ->setSetting('unsigned', TRUE); } + if ($entity_type->hasKey('revision_uuid')) { + $fields[$entity_type->getKey('revision_uuid')] = BaseFieldDefinition::create('uuid') + ->setLabel(new TranslatableMarkup('Revision UUID')) + ->setReadOnly(TRUE); + } if ($entity_type->hasKey('langcode')) { $fields[$entity_type->getKey('langcode')] = BaseFieldDefinition::create('language') ->setLabel(new TranslatableMarkup('Language')) diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php index 5acf92e..bae4165 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php @@ -7,7 +7,7 @@ namespace Drupal\Core\Entity\Sql; -use Drupal\Component\Uuid\Uuid; +use Drupal\Component\Uuid\UuidInterface; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Database\Connection; use Drupal\Core\Database\Database; @@ -141,7 +141,8 @@ public static function createInstance(ContainerInterface $container, EntityTypeI $container->get('database'), $container->get('entity.manager'), $container->get('cache.entity'), - $container->get('language_manager') + $container->get('language_manager'), + $container->get('uuid') ); } @@ -169,11 +170,14 @@ public function getFieldStorageDefinitions() { * The cache backend to be used. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. + * @param \Drupal\Component\Uuid\UuidInterface $uuid_service + * The UUID service. */ - public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager) { + public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, UuidInterface $uuid_service) { parent::__construct($entity_type, $entity_manager, $cache); $this->database = $database; $this->languageManager = $language_manager; + $this->uuidService = $uuid_service; $this->initTableLayout(); } @@ -333,7 +337,7 @@ public function getTableMapping(array $storage_definitions = NULL) { // denormalized in the base table but also stored in the revision table // together with the entity ID and the revision ID as identifiers. $table_mapping->setFieldNames($this->baseTable, array_diff($all_fields, $revision_metadata_fields)); - $revision_key_fields = array($this->idKey, $this->revisionKey, $this->revisionUuidKey); + $revision_key_fields = array($this->idKey, $this->revisionKey); $table_mapping->setFieldNames($this->revisionTable, array_merge($revision_key_fields, $revisionable_fields)); } elseif (!$revisionable && $translatable) { @@ -360,13 +364,13 @@ public function getTableMapping(array $storage_definitions = NULL) { // Like in the multilingual, non-revisionable case the UUID is not // in the data table. Additionally, do not store revision metadata // fields in the data table. - $data_fields = array_values(array_diff($all_fields, array($this->uuidKey), $revision_metadata_fields)); + $data_fields = array_values(array_diff($all_fields, array($this->uuidKey, $this->revisionUuidKey), $revision_metadata_fields)); $table_mapping->setFieldNames($this->dataTable, $data_fields); $revision_base_fields = array_merge(array($this->idKey, $this->revisionKey, $this->revisionUuidKey, $this->langcodeKey), $revision_metadata_fields); $table_mapping->setFieldNames($this->revisionTable, $revision_base_fields); - $revision_data_key_fields = array($this->idKey, $this->revisionKey, $this->revisionUuidKey, $this->langcodeKey); + $revision_data_key_fields = array($this->idKey, $this->revisionKey, $this->langcodeKey); $revision_data_fields = array_diff($revisionable_fields, $revision_metadata_fields, array($this->langcodeKey)); $table_mapping->setFieldNames($this->revisionDataTable, array_merge($revision_data_key_fields, $revision_data_fields)); } @@ -1038,11 +1042,13 @@ protected function saveRevision(ContentEntityInterface $entity) { // Generating a new revision should generate a new revision UUID. There // are cases where external sources may preset the revision UUID, so we // need to allow for that here. - if ($this->revisionUuidKey && (empty($record->{$this->revisionUuidKey}) - || (isset($entity->original) - && $entity->original->{$this->revisionUuidKey} == $record->{$this->revisionUuidKey}))) { - $uuid = new Uuid(); - $record->{$this->revisionUuidKey} = $uuid->generate(); + if ($this->revisionUuidKey) { + if (isset($entity->original) && $entity->original->{$this->revisionUuidKey} != $entity->original->{$this->revisionUuidKey}) { + $record->{$this->revisionUuidKey} = $entity->original->{$this->revisionUuidKey}->value; + } + else { + $record->{$this->revisionUuidKey} = $this->uuidService->generate(); + } } $insert_id = $this->database @@ -1056,7 +1062,10 @@ protected function saveRevision(ContentEntityInterface $entity) { } if ($entity->isDefaultRevision()) { $this->database->update($this->entityType->getBaseTable()) - ->fields(array($this->revisionKey => $record->{$this->revisionKey})) + ->fields([ + $this->revisionKey => $record->{$this->revisionKey}, + $this->revisionUuidKey => $record->{$this->revisionUuidKey}, + ]) ->condition($this->idKey, $record->{$this->idKey}) ->execute(); } @@ -1071,7 +1080,9 @@ protected function saveRevision(ContentEntityInterface $entity) { // Make sure to update the new revision ID and UUID keys for the entity. $entity->{$this->revisionKey}->value = $record->{$this->revisionKey}; - $entity->{$this->revisionUuidKey}->value = $record->{$this->revisionUuidKey}; + if ($this->revisionUuidKey) { + $entity->{$this->revisionUuidKey}->value = $record->{$this->revisionUuidKey}; + } return $record->{$this->revisionKey}; } diff --git a/core/modules/block_content/src/Entity/BlockContent.php b/core/modules/block_content/src/Entity/BlockContent.php index 2a05202..8de8511 100644 --- a/core/modules/block_content/src/Entity/BlockContent.php +++ b/core/modules/block_content/src/Entity/BlockContent.php @@ -52,7 +52,8 @@ * "bundle" = "type", * "label" = "info", * "langcode" = "langcode", - * "uuid" = "uuid" + * "uuid" = "uuid", + * "revision_uuid" = "revision_uuid" * }, * bundle_entity_type = "block_content_type", * field_ui_base_route = "entity.block_content_type.edit_form", @@ -166,6 +167,11 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setReadOnly(TRUE) ->setSetting('unsigned', TRUE); + $fields['revision_uuid'] = BaseFieldDefinition::create('uuid') + ->setLabel(t('UUID')) + ->setDescription(t('The revision UUID.')) + ->setReadOnly(TRUE); + $fields['langcode'] = BaseFieldDefinition::create('language') ->setLabel(t('Language')) ->setDescription(t('The custom block language code.')) diff --git a/core/modules/comment/src/CommentStorage.php b/core/modules/comment/src/CommentStorage.php index 5ed080f..c603c87 100644 --- a/core/modules/comment/src/CommentStorage.php +++ b/core/modules/comment/src/CommentStorage.php @@ -7,6 +7,7 @@ namespace Drupal\comment; +use Drupal\Component\Uuid\UuidInterface; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Database\Connection; use Drupal\Core\Entity\EntityManagerInterface; @@ -48,9 +49,11 @@ class CommentStorage extends SqlContentEntityStorage implements CommentStorageIn * Cache backend instance to use. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. + * @param \Drupal\Component\Uuid\UuidInterface $uuid_service + * The UUID service. */ - public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, AccountInterface $current_user, CacheBackendInterface $cache, LanguageManagerInterface $language_manager) { - parent::__construct($entity_info, $database, $entity_manager, $cache, $language_manager); + public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, AccountInterface $current_user, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, UuidInterface $uuid_service) { + parent::__construct($entity_info, $database, $entity_manager, $cache, $language_manager, $uuid_service); $this->currentUser = $current_user; } @@ -64,7 +67,8 @@ public static function createInstance(ContainerInterface $container, EntityTypeI $container->get('entity.manager'), $container->get('current_user'), $container->get('cache.entity'), - $container->get('language_manager') + $container->get('language_manager'), + $container->get('uuid') ); } diff --git a/core/modules/node/src/Entity/Node.php b/core/modules/node/src/Entity/Node.php index 7b20a3b..ddac388 100644 --- a/core/modules/node/src/Entity/Node.php +++ b/core/modules/node/src/Entity/Node.php @@ -53,6 +53,7 @@ * "label" = "title", * "langcode" = "langcode", * "uuid" = "uuid", + * "revision_uuid" = "revision_uuid", * "status" = "status", * "uid" = "uid", * },