Problem/Motivation

When deleting a group relationship the postDelete method retrieves the entity referenced from the relationship entity https://git.drupalcode.org/project/group/-/blob/1d7b4ee7d69b3bfcfde8cc79...
However if prior to this call the reference has been used to get the entity for whatever reason (just one example see steps to reproduce) it will be on the relationship entity even if in the database the referenced entity has since been deleted.

Steps to reproduce

Code I was using that stumbled on this was finding the entities to delete from their references. So:

$relationship = GroupRelationship::load($id);
$entity = $relationship->getEntity();
$entity->delete();

Run with $id being a gnode will cause errors:

[error]  Drupal\Core\Entity\EntityStorageException: Update existing 'node' entity while changing the ID is not supported. in Drupal\Core\Entity\ContentEntityStorageBase->doPreSave() (line 762 of /app/web/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php). 
 [error]  Drupal\Core\Entity\EntityStorageException: Update existing 'node' entity while changing the ID is not supported. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 815 of /app/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php). 
 [error]  Drupal\Core\Entity\EntityStorageException: Update existing 'node' entity while changing the ID is not supported. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->delete() (line 761 of /app/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php). 

As it tries to resave the already deleted entity.

Proposed resolution

Reloading the entity to check that it still exists works.

Remaining tasks

Patch/MR to follow.

Comments

ekes created an issue. See original summary.

ekes’s picture

StatusFileSize
new952 bytes

Patch first because I want it to apply to 2.0.x ;)
But believe this holds for everything from 2.0.x - 3.1.x The patch itself was made and tested against 3.0.x

maskedjellybean’s picture

StatusFileSize
new932 bytes

Wow thank you for this! In my case this error was being thrown during a Behat afterScenario while attempting to delete nodes. Here is a version of the patch for Group 1.0.x (tested against 1.6.0) for those of us still stuck on a super outdated version.