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.)