diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php index 23b0f4a..a363155 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Config\Entity; +use Drupal\Core\Config\Entity\Exception\ConfigEntityStorageClassException; use Drupal\Core\Entity\EntityType; use Drupal\Core\Config\ConfigPrefixLengthException; use Drupal\Component\Utility\String; @@ -62,6 +63,10 @@ class ConfigEntityType extends EntityType { /** * {@inheritdoc} + * + * @throws \Drupal\Core\Config\Entity\Exception\ConfigEntityStorageClassException + * Exception thrown when the provided class is not an instance of + * \Drupal\Core\Config\Entity\ConfigEntityStorage. */ public function __construct($definition) { // Ensure a default list cache tag is set; do this before calling the parent @@ -74,6 +79,9 @@ public function __construct($definition) { // Always add a default 'uuid' key. $this->entity_keys['uuid'] = 'uuid'; $this->entity_keys['langcode'] = 'langcode'; + if (isset($this->handlers['storage'])) { + $this->checkStorageClass($this->handlers['storage']); + } $this->handlers += array( 'storage' => 'Drupal\Core\Config\Entity\ConfigEntityStorage', ); @@ -139,4 +147,30 @@ public function getConfigDependencyKey() { return 'config'; } + /** + * {@inheritdoc} + * + * @throws \Drupal\Core\Config\Entity\Exception\ConfigEntityStorageClassException + * Exception thrown when the provided class is not an instance of + * \Drupal\Core\Config\Entity\ConfigEntityStorage. + */ + public function setStorageClass($class) { + $this->checkStorageClass($class); + parent::setStorageClass($class); + } + + /** + * Checks that the provided class is an instance of ConfigEntityStorage. + * + * @param string $class + * The class to check. + * + * @see \Drupal\Core\Config\Entity\ConfigEntityStorage. + */ + protected function checkStorageClass($class) { + if (!is_a($class, 'Drupal\Core\Config\Entity\ConfigEntityStorage', TRUE)) { + throw new ConfigEntityStorageClassException(String::format('@class is not \Drupal\Core\Config\Entity\ConfigEntityStorage or it does not extend it', ['@class' => $class])); + } + } + } diff --git a/core/lib/Drupal/Core/Config/Entity/Exception/ConfigEntityStorageClassException.php b/core/lib/Drupal/Core/Config/Entity/Exception/ConfigEntityStorageClassException.php new file mode 100644 index 0000000..d7c0f0c --- /dev/null +++ b/core/lib/Drupal/Core/Config/Entity/Exception/ConfigEntityStorageClassException.php @@ -0,0 +1,15 @@ +load($id); - } - - /** - * {@inheritdoc} - */ - public function loadMultipleOverrideFree(array $ids = NULL) { - // KeyValueEntityStorage does not support storing and retrieving overrides, - // it does not use the configuration factory. This is just a test method. - // See https://www.drupal.org/node/2393751. - return $this->loadMultiple($ids); - } - -} diff --git a/core/modules/search/src/Entity/SearchPage.php b/core/modules/search/src/Entity/SearchPage.php index 1a1af60..d8cfc41 100644 --- a/core/modules/search/src/Entity/SearchPage.php +++ b/core/modules/search/src/Entity/SearchPage.php @@ -24,7 +24,6 @@ * label = @Translation("Search page"), * handlers = { * "access" = "Drupal\search\SearchPageAccessControlHandler", - * "storage" = "Drupal\Core\Config\Entity\ConfigEntityStorage", * "list_builder" = "Drupal\search\SearchPageListBuilder", * "form" = { * "add" = "Drupal\search\Form\SearchPageAddForm", diff --git a/core/modules/system/src/Tests/KeyValueStore/KeyValueConfigEntityStorageTest.php b/core/modules/system/src/Tests/KeyValueStore/KeyValueConfigEntityStorageTest.php deleted file mode 100644 index 1fe1ab6..0000000 --- a/core/modules/system/src/Tests/KeyValueStore/KeyValueConfigEntityStorageTest.php +++ /dev/null @@ -1,32 +0,0 @@ -setStorageClass('Drupal\Core\Entity\KeyValueStore\KeyValueConfigEntityStorage'); - } if (isset($entity_types['entity_test_label'])) { $entity_types['entity_test_label']->setStorageClass('Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage'); $entity_keys = $entity_types['entity_test_label']->getKeys(); diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityTypeTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityTypeTest.php index 0af505d..d3dfc25 100644 --- a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityTypeTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityTypeTest.php @@ -75,4 +75,38 @@ public function testConfigPrefixLengthValid() { $this->assertEquals($expected_prefix, $config_entity->getConfigPrefix()); } + /** + * @covers ::__construct + */ + public function testConstruct() { + $config_entity = new ConfigEntityType([ + 'id' => 'example_config_entity_type', + ]); + $this->assertEquals('Drupal\Core\Config\Entity\ConfigEntityStorage', $config_entity->getStorageClass()); + } + + /** + * @covers ::__construct + * + * @expectedException \Drupal\Core\Config\Entity\Exception\ConfigEntityStorageClassException + * @expectedExceptionMessage \Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage is not \Drupal\Core\Config\Entity\ConfigEntityStorage or it does not extend it + */ + public function testConstructBadStorage() { + new ConfigEntityType([ + 'id' => 'example_config_entity_type', + 'handlers' => ['storage' => '\Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage'] + ]); + } + + /** + * @covers ::setStorageClass + * + * @expectedException \Drupal\Core\Config\Entity\Exception\ConfigEntityStorageClassException + * @expectedExceptionMessage \Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage is not \Drupal\Core\Config\Entity\ConfigEntityStorage or it does not extend it + */ + public function testSetStorageClass() { + $config_entity = $this->setUpConfigEntityType([]); + $config_entity->setStorageClass('\Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage'); + } + }