Verified Commit f2154d58 authored by Jess's avatar Jess
Browse files

Issue #3174924 by joachim, xjm, smustgrave, sourabhjain, ranjith_kumar_k_u,...

Issue #3174924 by joachim, xjm, smustgrave, sourabhjain, ranjith_kumar_k_u, kiwimind, catch, bnjmnm: Throw a better exception when a reference field can't find the target entity type
parent 78a7b476
......@@ -84,6 +84,9 @@ public static function mainPropertyName();
* stored in SQL. Also, the possible usage is limited, as you cannot
* specify another field as related, only existing SQL tables,
* such as {taxonomy_term_data}.
*
* @throws \Drupal\Core\Field\FieldException
* Throws an exception if the schema is invalid.
*/
public static function schema(FieldStorageDefinitionInterface $field_definition);
......
......@@ -6,6 +6,7 @@
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\ContentEntityStorageInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\TypedData\EntityDataDefinition;
......@@ -121,7 +122,16 @@ public static function mainPropertyName() {
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$target_type = $field_definition->getSetting('target_type');
$target_type_info = \Drupal::entityTypeManager()->getDefinition($target_type);
try {
$target_type_info = \Drupal::entityTypeManager()->getDefinition($target_type);
}
catch (PluginNotFoundException $e) {
throw new FieldException(sprintf("Field '%s' on entity type '%s' references a target entity type '%s' which does not exist.",
$field_definition->getName(),
$field_definition->getTargetEntityTypeId(),
$target_type
));
}
$properties = static::propertyDefinitions($field_definition)['target_id'];
if ($target_type_info->entityClassImplements(FieldableEntityInterface::class) && $properties->getDataType() === 'integer') {
$columns = [
......
......@@ -6,6 +6,7 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldException;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
......@@ -368,6 +369,26 @@ protected function assertUserRoleAutocreate(EntityInterface $entity, callable $s
$this->assertEquals($entity->user_role->target_id, $role->id());
}
/**
* Tests exception thrown with a missing target entity type.
*/
public function testTargetEntityTypeMissing() {
// Setup a test entity type with an entity reference field to an entity type
// that doesn't exist.
$definitions = [
'bad_reference' => BaseFieldDefinition::create('entity_reference')
->setSetting('target_type', 'made_up_entity_type')
->setSetting('handler', 'default'),
];
$this->state->set('entity_test.additional_base_field_definitions', $definitions);
$this->entityTypeManager->clearCachedDefinitions();
$this->expectException(FieldException::class);
$this->expectExceptionMessage("Field 'bad_reference' on entity type 'entity_test' references a target entity type 'made_up_entity_type' which does not exist.");
$this->installEntitySchema('entity_test');
}
/**
* Tests that the target entity is not unnecessarily loaded.
*/
......
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