diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index bbeceae211..40473b0d1f 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -342,13 +342,18 @@ public function wasDefaultRevision() { } $revision_default_key = $entity_type->getRevisionMetadataKey('revision_default'); - $value = $this->isNew() || $this->get($revision_default_key)->value; + $value = $this->isNew() ? TRUE : $this->get($revision_default_key)->value; if (isset($value)) { return (bool) $value; } else { - throw new \LogicException("Missing data for the '$revision_default_key' field on the entity {$this->getEntityTypeId()}:{$this->id()}."); + // This is not supposed to happen, since field data is populated + // automatically right before saving. If we have a corrupt database, we + // trigger an error, since the value we are returning may very well be + // wrong, although it is a good guess. + trigger_error("Missing data for the '$revision_default_key' field on the entity {$this->getEntityTypeId()}:{$this->id()}.", E_USER_ERROR); + return $this->isDefaultRevision(); } } diff --git a/core/lib/Drupal/Core/Entity/RevisionableInterface.php b/core/lib/Drupal/Core/Entity/RevisionableInterface.php index 5987580319..0fc6db2fec 100644 --- a/core/lib/Drupal/Core/Entity/RevisionableInterface.php +++ b/core/lib/Drupal/Core/Entity/RevisionableInterface.php @@ -56,9 +56,6 @@ public function isDefaultRevision($new_value = NULL); * * @return bool * TRUE if the entity object was a revision, FALSE otherwise. - * - * @throws \LogicException - * If the default revision data is missing. */ public function wasDefaultRevision(); diff --git a/core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php b/core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php index 3b9529ee6d..1f324a9e3c 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php @@ -140,6 +140,22 @@ public function testWasDefaultRevision() { /** @var \Drupal\entity_test_revlog\Entity\EntityTestMulWithRevisionLog $entity */ $entity = $storage->loadUnchanged($entity->id()); $this->assertTrue($entity->wasDefaultRevision()); + + // Simulate missing field data and verify an error is triggered in this + // case, while the return value is TRUE. + /** @var \Drupal\Core\Database\Connection $database */ + $database = $this->container->get('database'); + $database->update($entity_type_id . '_revision') + ->fields(['revision_default' => NULL]) + ->execute(); + $storage->resetCache([$entity->id()]); + $entity = $storage->load($entity->id()); + $entity->isDefaultRevision(TRUE); + $this->assertTrue(@$entity->wasDefaultRevision()); + $entity->isDefaultRevision(FALSE); + $this->assertFalse(@$entity->wasDefaultRevision()); + $this->setExpectedException('\PHPUnit_Framework_Error', "Missing data for the 'revision_default' field on the entity $entity_type_id:{$entity->id()}."); + $entity->wasDefaultRevision(); } /**