diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php index 11b66fc7a1939b23aa1ee7fb08ef5d2b47e2496f..62fb13efc0d840f87a822e2707e487650b66e65d 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php @@ -316,6 +316,13 @@ protected function has($id, EntityInterface $entity) { return !$config->isNew(); } + /** + * {@inheritdoc} + */ + public function hasData() { + return (bool) $this->configFactory->listAll($this->getPrefix()); + } + /** * Gets entities from the static cache. * diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index 9d6b8d64f5089560d505aa266d850076aec3564f..515db0d2f6d3aa249dcea2a9a75bf6eb7d07fb70 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -62,16 +62,6 @@ public static function createInstance(ContainerInterface $container, EntityTypeI ); } - /** - * {@inheritdoc} - */ - public function hasData() { - return (bool) $this->getQuery() - ->accessCheck(FALSE) - ->range(0, 1) - ->execute(); - } - /** * {@inheritdoc} */ diff --git a/core/lib/Drupal/Core/Entity/DynamicallyFieldableEntityStorageInterface.php b/core/lib/Drupal/Core/Entity/DynamicallyFieldableEntityStorageInterface.php index feed5653bb15d1876e62455d60a79008b6445e63..04d1b1e05c8fdc9caad05fc82070d498750aad7c 100644 --- a/core/lib/Drupal/Core/Entity/DynamicallyFieldableEntityStorageInterface.php +++ b/core/lib/Drupal/Core/Entity/DynamicallyFieldableEntityStorageInterface.php @@ -18,14 +18,6 @@ */ interface DynamicallyFieldableEntityStorageInterface extends FieldableEntityStorageInterface, FieldStorageDefinitionListenerInterface, FieldDefinitionListenerInterface { - /** - * Determines if the storage contains any data. - * - * @return bool - * TRUE if the storage contains data, FALSE if not. - */ - public function hasData(); - /** * Purges a batch of field data. * diff --git a/core/lib/Drupal/Core/Entity/EntityStorageBase.php b/core/lib/Drupal/Core/Entity/EntityStorageBase.php index 7335af119692ad24c18b42b5bf66c4fa6f8f1008..e5bf880aef2a89ffd259649cdfb08e8fcc756608 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageBase.php @@ -505,6 +505,16 @@ public function loadByProperties(array $values = []) { return $result ? $this->loadMultiple($result) : []; } + /** + * {@inheritdoc} + */ + public function hasData() { + return (bool) $this->getQuery() + ->accessCheck(FALSE) + ->range(0, 1) + ->execute(); + } + /** * {@inheritdoc} */ diff --git a/core/lib/Drupal/Core/Entity/EntityStorageInterface.php b/core/lib/Drupal/Core/Entity/EntityStorageInterface.php index 50166d6e27877febcc87c0ba6213456bdd63094a..9f50674d0df34c020ef398e0634bcaebb8886e60 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageInterface.php @@ -142,6 +142,14 @@ public function delete(array $entities); */ public function save(EntityInterface $entity); + /** + * Determines if the storage contains any data. + * + * @return bool + * TRUE if the storage contains data, FALSE if not. + */ + public function hasData(); + /** * Gets an entity query instance. * diff --git a/core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueEntityStorage.php b/core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueEntityStorage.php index de77e09d20f3ac94492900e3a65516592e1fdb1b..cd2f26efbd695a6d9720ea0ceea65a861576586e 100644 --- a/core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueEntityStorage.php +++ b/core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueEntityStorage.php @@ -187,6 +187,13 @@ protected function has($id, EntityInterface $entity) { return $this->keyValueStore->has($id); } + /** + * {@inheritdoc} + */ + public function hasData() { + return (bool) $this->keyValueStore->getAll(); + } + /** * {@inheritdoc} */ diff --git a/core/modules/system/tests/modules/keyvalue_test/keyvalue_test.module b/core/modules/system/tests/modules/keyvalue_test/keyvalue_test.module index 1e7da67fad926a303e69cba21600866d49da42f4..4316719dd3e4352bcce5cf317a201d529a95a646 100644 --- a/core/modules/system/tests/modules/keyvalue_test/keyvalue_test.module +++ b/core/modules/system/tests/modules/keyvalue_test/keyvalue_test.module @@ -14,5 +14,6 @@ function keyvalue_test_entity_type_alter(array &$entity_types) { $entity_types['entity_test_label']->setStorageClass('Drupal\Core\Entity\KeyValueStore\KeyValueContentEntityStorage'); $entity_keys = $entity_types['entity_test_label']->getKeys(); $entity_types['entity_test_label']->set('entity_keys', $entity_keys + ['uuid' => 'uuid']); + $entity_types['entity_test_label']->set('provider', 'keyvalue_test'); } } diff --git a/core/tests/Drupal/KernelTests/Core/Config/ConfigEntityStorageTest.php b/core/tests/Drupal/KernelTests/Core/Config/ConfigEntityStorageTest.php index bf41e1de505078b1dfe6656a9cdd4f0a7a3b4fe4..ce9a928d53a385157f7cd8903c32c7a0a8ca3482 100644 --- a/core/tests/Drupal/KernelTests/Core/Config/ConfigEntityStorageTest.php +++ b/core/tests/Drupal/KernelTests/Core/Config/ConfigEntityStorageTest.php @@ -51,4 +51,18 @@ public function testUUIDConflict() { $this->assertIdentical($entity->toArray(), $original_properties); } + /** + * Tests the hasData() method for config entity storage. + * + * @covers \Drupal\Core\Config\Entity\ConfigEntityStorage::hasData + */ + public function testHasData() { + $storage = \Drupal::entityTypeManager()->getStorage('config_test'); + $this->assertFalse($storage->hasData()); + + // Add a test config entity and check again. + $storage->create(['id' => $this->randomMachineName()])->save(); + $this->assertTrue($storage->hasData()); + } + } diff --git a/core/tests/Drupal/KernelTests/Core/KeyValueStore/KeyValueContentEntityStorageTest.php b/core/tests/Drupal/KernelTests/Core/KeyValueStore/KeyValueContentEntityStorageTest.php index 31300dbcd77a46eecbcc2d0a931380e34b683b32..98850055a5fb92195d9c0d64ba96cb67fe92f743 100644 --- a/core/tests/Drupal/KernelTests/Core/KeyValueStore/KeyValueContentEntityStorageTest.php +++ b/core/tests/Drupal/KernelTests/Core/KeyValueStore/KeyValueContentEntityStorageTest.php @@ -31,9 +31,15 @@ protected function setUp() { /** * Tests CRUD operations. + * + * @covers \Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage::hasData */ public function testCRUD() { $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId(); + + $storage = \Drupal::entityTypeManager()->getStorage('entity_test_label'); + $this->assertFalse($storage->hasData()); + // Verify default properties on a newly created empty entity. $empty = EntityTestLabel::create(); $this->assertIdentical($empty->id->value, NULL); @@ -108,6 +114,9 @@ public function testCRUD() { $this->fail('EntityMalformedException was not thrown.'); } + // Verify that hasData() returns the expected result. + $this->assertTrue($storage->hasData()); + // Verify that the correct status is returned and properties did not change. $this->assertIdentical($status, SAVED_NEW); $this->assertIdentical($entity_test->id(), $expected['id']); @@ -157,4 +166,12 @@ public function testCRUD() { } } + /** + * Tests uninstallation of a module that does not use the SQL entity storage. + */ + public function testUninstall() { + $uninstall_validator_reasons = \Drupal::service('content_uninstall_validator')->validate('keyvalue_test'); + $this->assertEmpty($uninstall_validator_reasons); + } + }