Fatal error when updating entity reference field storage config, after removing target entity type class from codebase

Created on 29 March 2019, over 5 years ago
Updated 21 September 2024, 3 months ago

As part of a module update, we're renaming a number of entity types, in order to follow Drupal's naming conventions. The entity types are used in entity reference fields.

In order to update the entity reference fields' storage configuration (to change the target_type of the entity reference field), I'm running the following in an update function:

$field_storage_config = FieldStorageConfig::loadByName('entity_type', 'field_name');
$field_storage_config->setSetting('target_type', 'new_entity_type');
$field_storage_config->save();

When I run the update using drush updb, I get a fatal error saying the previously used target entity type (the one that's been removed) doesn't exist (this is the PluginNotFoundException thrown in \Drupal\Core\Entity\EntityTypeManager, line 149). When I run the update using the update UI, the update fails with an Ajax error (callstack attached).

It seems that the $field_storage_config->save() method eventually calls \Drupal\Core\Entity\EntityTypeManager::getDefinition for the removed entity type, leading to the fatal error.

(A current workaround, as described on https://drupal.stackexchange.com/a/278355/564, is to leave the previous target entity types in place and mark them deprecated, and remove them in a subsequent update. This has a chance of breaking sites though, if people skip a module version.)

πŸ› Bug report
Status

Closed: works as designed

Version

11.0 πŸ”₯

Component
EntityΒ  β†’

Last updated about 17 hours ago

Created by

πŸ‡¨πŸ‡¦Canada brunodbo

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