- Issue created by @ovilla
- 🇩🇪Germany jurgenhaas Gottmadingen
All this relies on the fact, that the process being executed by Drupal allows others to hook into that process and use any provided method to interrupt that original process under certain conditions.
Using an exception would be such an interruption, but with all the consequences, that this actually crashes the whole process. That's very unlikely that you want that to happen.
When an entity gets created through a form, Drupal has that form validation phase, which is used BEFORE creating and saving the entity. This is where you can hook in with ECA and set a form error, which would then prevent the creation from being done.
If your entity is created by some other means, with a module or with some custom code, that process would have to allow such a validation before creation. If it doesn't, nothing on earth has a chance to prevent the entity from being created and saved, even if your business rules don't want to allow that.
In that case, you're down to your approach to check for duplicates after the event, and then delete that new one again when the rules apply. Now, you tried deleting that entity during the pre-save event, and that's just not possible. When Drupal goes into saving an entity, it follows along. There is nothing that could or should stop that process from finishing, other than an exception. But we've ruled that out above already.
So, you need to use the Insert content entity event. That one is dispatched when the saving by Drupal is completed, and then you can apply your business rules.
Automatically closed - issue fixed for 2 weeks with no activity.