Problem/Motivation
When creating an entity in a group through the create form, CreateFormEnhancer::submitForm() runs as a submit handler after the entity form's save handler. Save handlers like \Drupal\node\Form\NodeForm::save() catch storage exceptions instead of re-throwing them, show an error message and request a form rebuild, so the enhancer's submit handler still runs even though the entity was never persisted.
If the exception was thrown after the base table insert (for example in a hook_node_insert() implementation), the transaction is rolled back but the in-memory entity keeps its assigned ID, so a relationship is created pointing to an entity that does not exist. GroupRelationship::postSave() then fatals calling getCacheTags() on NULL via getEntity(). If the relationship save is skipped, a second fatal surfaces on the form rebuild: the enhancer attaches itself to the form as a validate/submit callable, ends up in the form cache, and fails to serialize ("The database connection is not serializable").
Proposed resolution
Skip the relationship creation in CreateFormEnhancer::submitForm() when the entity is still new or the form is being rebuilt, and add DependencySerializationTrait to CreateFormEnhancer so enhanced forms can be cached.
Remaining tasks
Review.
Issue fork group-3595517
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
amateescu commentedComment #5
gabesulliceLooks good and works well for me :)