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.)
Comment | File | Size | Author |
---|---|---|---|
callstack.txt | 4.33 KB | brunodbo |
Comments
Comment #2
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedThis was recently fixed by #2976035: Entity type CRUD operations must use the last installed entity type and field storage definitions and #2554235: Make the content entity storage and entity query use the last installed definitions instead of the ones living in code, can you try again with 8.7.0-beta1?
Comment #3
brunodboI'm getting the same error with 8.7.0-beta1.
If I run the update code from the issue summary when the entity type class is present in code, the entity reference field's target type is updated correctly; once I delete the entity type class file, I get the fatal error.
Comment #4
brunodboComment #11
larowlanAccording to the docs, its not safe to use the entity api in update hooks, and this should instead be done in a post update hook.
Does the same thing occur in that scenario?
Comment #12
smustgrave CreditAttribution: smustgrave at Mobomo commentedClosed #2929881: Changing the target type of an entity reference does not trigger an field definition update, leading to broken sites as a duplicate