diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 12b9324402..0c58b0d31a 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -335,14 +335,17 @@ public function isDefaultRevision($new_value = NULL) { * {@inheritdoc} */ public function wasDefaultRevision() { - if (!$this->getEntityType()->isRevisionable()) { + /** @var \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type */ + $entity_type = $this->getEntityType(); + if (!$entity_type->isRevisionable()) { throw new \LogicException("Entity type {$this->getEntityTypeId()} does not support revisions."); } - $value = $this->get('default_revision')->value; + $revision_default_key = $entity_type->getRevisionMetadataKey('revision_default'); + $value = $this->hasField($revision_default_key) ? $this->get($revision_default_key)->value : NULL; if (isset($value)) { return (bool) $value; } - throw new \LogicException("Missing data for the \"default_revision\" field on the entity {$this->getEntityTypeId()}:{$this->id()}."); + throw new \LogicException("Missing data for the \"{$revision_default_key}\" field on the entity {$this->getEntityTypeId()}:{$this->id()}."); } /** @@ -1302,12 +1305,6 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setLabel(new TranslatableMarkup('Revision ID')) ->setReadOnly(TRUE) ->setSetting('unsigned', TRUE); - - $fields['default_revision'] = BaseFieldDefinition::create('boolean') - ->setLabel(new TranslatableMarkup('Default revision')) - ->setDescription(new TranslatableMarkup('A flag indicating whether this was a default revision when it was saved.')) - ->setTranslatable(FALSE) - ->setRevisionable(TRUE); } if ($entity_type->hasKey('langcode')) { diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index 8803d36802..e402a174f4 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -334,9 +334,12 @@ protected function doSave($id, EntityInterface $entity) { } $this->populateAffectedRevisionTranslations($entity); - if ($this->entityType->isRevisionable()) { - $entity->set('default_revision', $entity->isDefaultRevision()); + + // Populate the "revision_default" flag. + if ($this->entityType->isRevisionable() && ($revision_default_key = $this->entityType->getRevisionMetadataKey('revision_default')) && $entity->hasField($revision_default_key)) { + $entity->set($revision_default_key, $entity->isDefaultRevision()); } + $this->doSaveFieldItems($entity); return $return; diff --git a/core/lib/Drupal/Core/Entity/ContentEntityType.php b/core/lib/Drupal/Core/Entity/ContentEntityType.php index 0e26c3bb51..1c2b391015 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityType.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityType.php @@ -54,7 +54,7 @@ protected function checkStorageClass($class) { public function getRevisionMetadataKeys($include_backwards_compatibility_field_names = TRUE) { // Provide backwards compatibility in case the revision metadata keys are // not defined in the entity annotation. - if (!$this->revision_metadata_keys && $include_backwards_compatibility_field_names) { + if ($include_backwards_compatibility_field_names && count($this->revision_metadata_keys) < 4) { $base_fields = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions($this->id()); if ((isset($base_fields['revision_uid']) && $revision_user = 'revision_uid') || (isset($base_fields['revision_user']) && $revision_user = 'revision_user')) { @trigger_error('The revision_user revision metadata key is not set.', E_USER_DEPRECATED); @@ -68,6 +68,10 @@ public function getRevisionMetadataKeys($include_backwards_compatibility_field_n @trigger_error('The revision_log_message revision metadata key is not set.', E_USER_DEPRECATED); $this->revision_metadata_keys['revision_log_message'] = $revision_log; } + if (isset($base_fields['revision_default'])) { + @trigger_error('The revision_default revision metadata key is not set.', E_USER_DEPRECATED); + $this->revision_metadata_keys['revision_default'] = 'revision_default'; + } } return $this->revision_metadata_keys; } diff --git a/core/lib/Drupal/Core/Entity/RevisionLogEntityTrait.php b/core/lib/Drupal/Core/Entity/RevisionLogEntityTrait.php index f935a8b510..57ddb167dd 100644 --- a/core/lib/Drupal/Core/Entity/RevisionLogEntityTrait.php +++ b/core/lib/Drupal/Core/Entity/RevisionLogEntityTrait.php @@ -3,6 +3,7 @@ namespace Drupal\Core\Entity; use Drupal\Core\Field\BaseFieldDefinition; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\user\UserInterface; /** @@ -49,6 +50,12 @@ public static function revisionLogBaseFieldDefinitions(EntityTypeInterface $enti ], ]); + $fields[static::getRevisionMetadataKey($entity_type, 'revision_default')] = BaseFieldDefinition::create('boolean') + ->setLabel(new TranslatableMarkup('Default revision')) + ->setDescription(new TranslatableMarkup('A flag indicating whether this was a default revision when it was saved.')) + ->setTranslatable(FALSE) + ->setRevisionable(TRUE); + return $fields; } @@ -133,6 +140,7 @@ protected static function getRevisionMetadataKey(EntityTypeInterface $entity_typ 'revision_created' => 'revision_created', 'revision_user' => 'revision_user', 'revision_log_message' => 'revision_log_message', + 'revision_default' => 'revision_default', ]; return $revision_metadata_keys[$key]; diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchemaConverter.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchemaConverter.php index 3b15161380..3d8ddc2ac2 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchemaConverter.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchemaConverter.php @@ -246,6 +246,7 @@ protected function copyData(array &$sandbox) { $revision_id_key = $temporary_entity_type->getKey('revision'); $revision_translation_affected_key = $temporary_entity_type->getKey('revision_translation_affected'); + $revision_default_key = $temporary_entity_type->getRevisionMetadataKey('revision_default'); // If 'progress' is not set, then this will be the first run of the batch. if (!isset($sandbox['progress'])) { @@ -283,6 +284,8 @@ protected function copyData(array &$sandbox) { $storage->setEntityType($temporary_entity_type); $storage->setTableMapping($temporary_table_mapping); + /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ + $has_revision_default_key = TRUE; foreach ($entities as $entity_id => $entity) { try { // Set the revision ID to be same as the entity ID. @@ -295,7 +298,9 @@ protected function copyData(array &$sandbox) { // We cannot know what type the revision was so we assume the core // default behavior of always creating a default revision. - $entity->set('default_revision', TRUE); + if ($has_revision_default_key && ($has_revision_default_key = $entity->hasField($revision_default_key))) { + $entity->set($revision_default_key, TRUE); + } // Treat the entity as new in order to make the storage do an INSERT // rather than an UPDATE. @@ -315,7 +320,7 @@ protected function copyData(array &$sandbox) { } // Re-throw the original exception with a helpful message. - throw new EntityStorageException("The entity update process failed while processing the entity {$original_entity_type->id()}:$entity_id.", $e->getCode(), $e); + throw new EntityStorageException("The entity update process failed while processing the entity {$original_entity_type->id()}:$entity_id." . $e, $e->getCode(), $e); } $sandbox['progress']++; @@ -384,20 +389,6 @@ protected function updateFieldStorageDefinitionsToRevisionable(ContentEntityType } $updated_storage_definitions[$entity_type->getKey('revision')] = $revision_field; - // Add the default revision flag. - $field_name = 'default_revision'; - $default_revision_field = BaseFieldDefinition::create('boolean') - ->setName($field_name) - ->setLabel(new TranslatableMarkup('Default revision')) - ->setDescription(new TranslatableMarkup('A flag indicating whether this was a default revision when it was saved.')) - ->setTranslatable(FALSE) - ->setRevisionable(TRUE); - - if ($update_cached_definitions) { - $this->entityDefinitionUpdateManager->installFieldStorageDefinition($default_revision_field->getName(), $entity_type->id(), $entity_type->getProvider(), $default_revision_field); - } - $updated_storage_definitions[$default_revision_field->getName()] = $default_revision_field; - // Add the 'revision_translation_affected' field if needed. if ($entity_type->isTranslatable()) { $revision_translation_affected_field = BaseFieldDefinition::create('boolean') diff --git a/core/modules/block_content/src/Entity/BlockContent.php b/core/modules/block_content/src/Entity/BlockContent.php index 3c8858ff88..eb10f97108 100644 --- a/core/modules/block_content/src/Entity/BlockContent.php +++ b/core/modules/block_content/src/Entity/BlockContent.php @@ -56,7 +56,8 @@ * revision_metadata_keys = { * "revision_user" = "revision_user", * "revision_created" = "revision_created", - * "revision_log_message" = "revision_log" + * "revision_log_message" = "revision_log", + * "revision_default" = "revision_default", * }, * bundle_entity_type = "block_content_type", * field_ui_base_route = "entity.block_content_type.edit_form", diff --git a/core/modules/media/src/Entity/Media.php b/core/modules/media/src/Entity/Media.php index 49f7283f7e..6fa4067d5a 100644 --- a/core/modules/media/src/Entity/Media.php +++ b/core/modules/media/src/Entity/Media.php @@ -64,6 +64,7 @@ * "revision_user" = "revision_user", * "revision_created" = "revision_created", * "revision_log_message" = "revision_log_message", + * "revision_default" = "revision_default", * }, * bundle_entity_type = "media_type", * permission_granularity = "entity_type", diff --git a/core/modules/node/src/Entity/Node.php b/core/modules/node/src/Entity/Node.php index 368ce1b596..2a1907085a 100644 --- a/core/modules/node/src/Entity/Node.php +++ b/core/modules/node/src/Entity/Node.php @@ -62,7 +62,8 @@ * revision_metadata_keys = { * "revision_user" = "revision_uid", * "revision_created" = "revision_timestamp", - * "revision_log_message" = "revision_log" + * "revision_log_message" = "revision_log", + * "revision_default" = "revision_default", * }, * bundle_entity_type = "node_type", * field_ui_base_route = "entity.node_type.edit_form", diff --git a/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentResourceTestBase.php index c652a2c4be..71632761d9 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentResourceTestBase.php @@ -121,7 +121,7 @@ protected function getExpectedNormalizedEntity() { 'value' => TRUE, ], ], - 'default_revision' => [ + 'revision_default' => [ [ 'value' => TRUE, ], diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php index b84c96632e..430b731be7 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php @@ -212,7 +212,7 @@ protected function getExpectedNormalizedEntity() { 'value' => TRUE, ], ], - 'default_revision' => [ + 'revision_default' => [ [ 'value' => TRUE, ], diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php index fa972eaa6a..0592cab547 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php @@ -152,7 +152,7 @@ protected function getExpectedNormalizedEntity() { 'value' => TRUE, ], ], - 'default_revision' => [ + 'revision_default' => [ [ 'value' => TRUE, ], diff --git a/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php b/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php index 88039ba799..f389336621 100644 --- a/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php +++ b/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php @@ -129,7 +129,7 @@ public function testNormalize() { 'revision_translation_affected' => [ ['value' => TRUE], ], - 'default_revision' => [ + 'revision_default' => [ ['value' => TRUE], ], 'non_rev_field' => [], @@ -204,7 +204,7 @@ public function testSerialize() { 'revision_id' => '' . $this->entity->getRevisionId() . '', 'default_langcode' => '1', 'revision_translation_affected' => '1', - 'default_revision' => '1', + 'revision_default' => '1', 'non_rev_field' => '', 'field_test_text' => '' . $this->values['field_test_text']['value'] . '' . $this->values['field_test_text']['format'] . '', ]; diff --git a/core/modules/system/system.install b/core/modules/system/system.install index 38c63517f7..e1b9358bcd 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -2058,7 +2058,7 @@ function system_update_8403() { } /** - * Add the 'revision_type' field to all entity types. + * Add the 'revision_default' field to all relevant entity types. */ function system_update_8501() { /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */ @@ -2070,8 +2070,9 @@ function system_update_8501() { $definitions = array_filter(\Drupal::entityTypeManager()->getDefinitions(), function (EntityTypeInterface $entity_type) use ($field_manager, $definition_update_manager) { $entity_type_id = $entity_type->id(); if ($entity_type = $definition_update_manager->getEntityType($entity_type_id)) { - $field_name = 'default_revision'; - return $entity_type->isRevisionable() + return $entity_type instanceof ContentEntityTypeInterface + && $entity_type->isRevisionable() + && ($field_name = $entity_type->getRevisionMetadataKey('revision_default')) && isset($field_manager->getFieldStorageDefinitions($entity_type_id)[$field_name]) && !$definition_update_manager->getFieldStorageDefinition($field_name, $entity_type_id); } @@ -2085,9 +2086,9 @@ function system_update_8501() { ->setRevisionable(TRUE) ->setInitialValue(TRUE); - // Install the 'default_revision' field. + // Install the 'revision_default' field. foreach ($definitions as $entity_type_id => $entity_type) { - $field_name = 'default_revision'; + $field_name = $entity_type->getRevisionMetadataKey('revision_default'); $definition_update_manager ->installFieldStorageDefinition($field_name, $entity_type_id, $entity_type_id, $storage_definition); } diff --git a/core/modules/system/tests/modules/entity_test_update/src/Entity/EntityTestUpdate.php b/core/modules/system/tests/modules/entity_test_update/src/Entity/EntityTestUpdate.php index 175bc7ad80..286e8e62bd 100644 --- a/core/modules/system/tests/modules/entity_test_update/src/Entity/EntityTestUpdate.php +++ b/core/modules/system/tests/modules/entity_test_update/src/Entity/EntityTestUpdate.php @@ -69,17 +69,11 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setDescription(new TranslatableMarkup('The bundle of the test entity.')) ->setRequired(TRUE); - if ($entity_type->isRevisionable()) { + if ($entity_type->hasKey('revision')) { $fields[$entity_type->getKey('revision')] = BaseFieldDefinition::create('integer') ->setLabel(new TranslatableMarkup('Revision ID')) ->setReadOnly(TRUE) ->setSetting('unsigned', TRUE); - - $fields['default_revision'] = BaseFieldDefinition::create('boolean') - ->setLabel(new TranslatableMarkup('Default revision')) - ->setDescription(new TranslatableMarkup('A flag indicating whether this was a default revision when it was saved.')) - ->setTranslatable(FALSE) - ->setRevisionable(TRUE); } $fields[$entity_type->getKey('langcode')] = BaseFieldDefinition::create('language')