Unverified Commit cac70bf7 authored by alexpott's avatar alexpott
Browse files

Issue #3261538 by andregp, dww, joachim, alexpott:...

Issue #3261538 by andregp, dww, joachim, alexpott: BundleClassInheritanceException incorrectly thrown when a bundle class does not exist

(cherry picked from commit 2b1faaac)
parent a5c0f164
......@@ -7,6 +7,7 @@
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Entity\Exception\AmbiguousBundleClassException;
use Drupal\Core\Entity\Exception\BundleClassInheritanceException;
use Drupal\Core\Entity\Exception\MissingBundleClassException;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
......@@ -202,9 +203,12 @@ public function getEntityClass(?string $bundle = NULL): string {
$bundle_info = $this->entityTypeBundleInfo->getBundleInfo($this->entityTypeId);
$bundle_class = $bundle_info[$bundle]['class'] ?? NULL;
// Bundle classes should extend the main entity class.
// Bundle classes should exist and extend the main entity class.
if ($bundle_class) {
if (!is_subclass_of($bundle_class, $entity_class)) {
if (!class_exists($bundle_class)) {
throw new MissingBundleClassException($bundle_class);
}
elseif (!is_subclass_of($bundle_class, $entity_class)) {
throw new BundleClassInheritanceException($bundle_class, $entity_class);
}
return $bundle_class;
......
<?php
namespace Drupal\Core\Entity\Exception;
/**
* Exception thrown if a bundle class does not exist.
*
* @see \Drupal\Core\Entity\ContentEntityStorageBase::getEntityClass()
*/
class MissingBundleClassException extends \Exception {
/**
* Constructs a MissingBundleClassException.
*
* @param string $bundle_class
* The bundle class which should exist.
*/
public function __construct(string $bundle_class) {
$message = sprintf('Bundle class %s does not exist.', $bundle_class);
parent::__construct($message);
}
}
......@@ -32,6 +32,10 @@ function entity_test_bundle_class_entity_bundle_info_alter(&$bundles) {
if (\Drupal::state()->get('entity_test_bundle_class_enable_user_class', FALSE)) {
$bundles['user']['user']['class'] = EntityTestUserClass::class;
}
if (\Drupal::state()->get('entity_test_bundle_class_does_not_exist', FALSE)) {
$bundles['entity_test']['bundle_class']['class'] = '\Drupal\Core\NonExistentClass';
}
}
/**
......
......@@ -4,6 +4,7 @@
use Drupal\Core\Entity\Exception\AmbiguousBundleClassException;
use Drupal\Core\Entity\Exception\BundleClassInheritanceException;
use Drupal\Core\Entity\Exception\MissingBundleClassException;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\entity_test_bundle_class\Entity\EntityTestAmbiguousBundleClass;
use Drupal\entity_test_bundle_class\Entity\EntityTestBundleClass;
......@@ -249,6 +250,17 @@ public function testBundleClassShouldExtendEntityClass() {
$this->storage->create(['type' => 'bundle_class']);
}
/**
* Checks exception thrown if a bundle class doesn't exist.
*/
public function testBundleClassShouldExist() {
$this->container->get('state')->set('entity_test_bundle_class_does_not_exist', TRUE);
$this->entityTypeManager->clearCachedDefinitions();
$this->expectException(MissingBundleClassException::class);
entity_test_create_bundle('bundle_class');
$this->storage->create(['type' => 'bundle_class']);
}
/**
* Tests that a module can override an entity-type class.
*
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment