Throw a better exception when a reference field can't find the target entity type

Created on 5 October 2020, about 4 years ago
Updated 26 January 2023, almost 2 years ago

Problem/Motivation

If you define an entity reference field as a base field in code, and the entity type that the field references doesn't exist, you get an exception about the missing entity type. But the exception message doesn't tell you anything about the field.

This means that a developer who has messed up their field definitions, or who hasn't enabled all the right modules in a Kernel test, doesn't know where to look for the problem.

Steps to reproduce

Either:
- Define an entity reference base field on an entity type with a made-up referenced entity type
- Create a kernel test, enable a module that provides and entity type with an entity reference base field, but don't enable the module that provides the referenced entity type

You get this exception:

> Drupal\Component\Plugin\Exception\PluginNotFoundException: The "taxonomy_term" entity type does not exist.

Proposed resolution

EntityReferenceItem should catch this exception and throw a FieldException instead, stating the location of the problem, e.g.:

> Drupal\Core\Field\FieldException: Field 'foo' on entity type 'my_entity' references a target entity type 'taxonomy_term' which does not exist.

Remaining tasks

Do it.

Update any tests that expect the exception.

User interface changes

None.

API changes

A different type of exception is now thrown.

Data model changes

None.

Release notes snippet

📌 Task
Status

Fixed

Version

10.1

Component
Entity 

Last updated 2 days ago

Created by

🇬🇧United Kingdom joachim

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024