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

CommentFileSizeAuthor
callstack.txt4.33 KBbrunodbo
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

brunodbo created an issue. See original summary.

amateescu’s picture

Component: configuration system » entity system
Status: Active » Postponed (maintainer needs more info)
Issue tags: -Entity Reference
brunodbo’s picture

I'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.

brunodbo’s picture

Status: Postponed (maintainer needs more info) » Active

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.9 was released on November 6 and is the final full bugfix release for the Drupal 8.7.x series. Drupal 8.7.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.8.0 on December 4, 2019. (Drupal 8.8.0-beta1 is available for testing.)

Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

larowlan’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: +Bug Smash Initiative

According 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?

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.