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.