diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 7f7c127efb..5b821c8de0 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -335,11 +335,10 @@ public function isDefaultRevision($new_value = NULL) { * {@inheritdoc} */ public function wasDefaultRevision() { - $value = $this->get('default_revision')->value; - if (isset($value)) { - return (bool) $value; + if (!$this->getEntityType()->isRevisionable()) { + throw new \LogicException("Entity type {$this->getEntityTypeId()} does not support revisions."); } - throw new \LogicException('The "default_revision" field data is missing for entity ' . (int) $this->id() . '.'); + return (bool) $this->get('default_revision')->value; } /** @@ -1280,23 +1279,34 @@ protected function getEntityKey($key) { */ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields = []; + if ($entity_type->hasKey('id')) { $fields[$entity_type->getKey('id')] = BaseFieldDefinition::create('integer') ->setLabel(new TranslatableMarkup('ID')) ->setReadOnly(TRUE) ->setSetting('unsigned', TRUE); } + if ($entity_type->hasKey('uuid')) { $fields[$entity_type->getKey('uuid')] = BaseFieldDefinition::create('uuid') ->setLabel(new TranslatableMarkup('UUID')) ->setReadOnly(TRUE); } - if ($entity_type->hasKey('revision')) { + + if ($entity_type->isRevisionable()) { $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) + ->setRequired(TRUE); } + if ($entity_type->hasKey('langcode')) { $fields[$entity_type->getKey('langcode')] = BaseFieldDefinition::create('language') ->setLabel(new TranslatableMarkup('Language')) @@ -1314,6 +1324,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields[$entity_type->getKey('langcode')]->setTranslatable(TRUE); } } + if ($entity_type->hasKey('bundle')) { if ($bundle_entity_type_id = $entity_type->getBundleEntityType()) { $fields[$entity_type->getKey('bundle')] = BaseFieldDefinition::create('entity_reference') diff --git a/core/lib/Drupal/Core/Entity/ContentEntityInterface.php b/core/lib/Drupal/Core/Entity/ContentEntityInterface.php index bb0b8ef9bb..e85a299312 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityInterface.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityInterface.php @@ -111,6 +111,10 @@ public function updateLoadedRevisionId(); * * @return bool * TRUE if the entity object was a revision, FALSE otherwise. + * + * @throws \LogicException + * If the entity type is not revisionable or the default revision data is + * missing. */ public function wasDefaultRevision(); diff --git a/core/lib/Drupal/Core/Entity/EntityFieldManager.php b/core/lib/Drupal/Core/Entity/EntityFieldManager.php index f32f672d77..040a34c681 100644 --- a/core/lib/Drupal/Core/Entity/EntityFieldManager.php +++ b/core/lib/Drupal/Core/Entity/EntityFieldManager.php @@ -220,16 +220,6 @@ protected function buildBaseFieldDefinitions($entity_type_id) { } } - // Make sure that revisionable entity types are correctly defined. - if ($entity_type->isRevisionable()) { - $base_field_definitions['default_revision'] = BaseFieldDefinition::create('boolean') - ->setLabel($this->t('Default revision')) - ->setDescription($this->t('A flag indicating whether this was a default revision when it was saved.')) - ->setTranslatable(FALSE) - ->setRevisionable(TRUE) - ->setDefaultValue(TRUE); - } - // Make sure that revisionable and translatable entity types are correctly // defined. if ($entity_type->isRevisionable() && $entity_type->isTranslatable()) { diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchemaConverter.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchemaConverter.php index ba6e1988be..fb497523bd 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchemaConverter.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchemaConverter.php @@ -390,7 +390,7 @@ protected function updateFieldStorageDefinitionsToRevisionable(ContentEntityType ->setDescription(new TranslatableMarkup('A flag indicating whether this was a default revision when it was saved.')) ->setTranslatable(FALSE) ->setRevisionable(TRUE) - ->setDefaultValue(TRUE); + ->setRequired(TRUE); if ($update_cached_definitions) { $this->entityDefinitionUpdateManager->installFieldStorageDefinition($revision_type_field->getName(), $entity_type->id(), $entity_type->getProvider(), $revision_type_field); diff --git a/core/modules/system/system.install b/core/modules/system/system.install index c7a4ad4654..2f59ad4255 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -2082,7 +2082,7 @@ function system_update_8501() { ->setDescription(t('A flag indicating whether this was a default revision when it was saved.')) ->setTranslatable(FALSE) ->setRevisionable(TRUE) - ->setDefaultValue(TRUE) + ->setRequired(TRUE) ->setInitialValue(TRUE); $definition_update_manager diff --git a/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php b/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php index a36d04dc15..4aca50032b 100644 --- a/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php +++ b/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php @@ -374,6 +374,7 @@ protected function runUpdates() { // Ensure that the update hooks updated all entity schema. $needs_updates = \Drupal::entityDefinitionUpdateManager()->needsUpdates(); + $this->assertFalse($needs_updates, 'After all updates ran, entity schema is up to date.'); if ($needs_updates) { foreach (\Drupal::entityDefinitionUpdateManager() ->getChangeSummary() as $entity_type_id => $summary) { @@ -382,7 +383,6 @@ protected function runUpdates() { } } } - $this->assertFalse($needs_updates, 'After all updates ran, entity schema is up to date.'); } } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php index 348c33cbb8..fa55935bba 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php @@ -113,7 +113,6 @@ public function testEntityTypeUpdateWithoutData() { // The revision key is now defined, so the revision field needs to be // created. t('The %field_name field needs to be installed.', ['%field_name' => 'Revision ID']), - t('The %field_name field needs to be installed.', ['%field_name' => 'Default revision']), ], ]; $this->assertEqual($this->entityDefinitionUpdateManager->getChangeSummary(), $expected, 'EntityDefinitionUpdateManager reports the expected change summary.');