How can I prevent an entity from being created?

Created on 20 September 2024, 6 months ago

In a site I am using the Storage Entities module, through ECA and Feeds I generate some entities of that type, I would like to avoid that duplicate entities are generated based on the verification of 1 or 2 fields, so I have made some tests to avoid that entities are generated when they are going to be duplicated, at the beginning I tried to do it with an ECA model in which I put the presave event and through conditions I verified that it was a new record, besides verifying that there was not another entity with the same value in a certain field, if that happened I executed an action to delete the entity, but that causes me an error which tells me that I do not have permission to delete the entity, I tried to include an action to make user switch to one that I know that has permission to do so and still I get that error.

I also tried using ECA Access with the event to determine if it has access to create an entity, I used conditions similar to those described above and if they were fulfilled it executed an action to forbid the access, but nothing happens, it seems that the event is not launched, I even tried to send a message to the user right after that event occurs, but at no time I see that message when I try to create an entity with the properties that I indicated.

When I searched in general how I can do what I need even if it is not with ECA, I found this post https://drupal.stackexchange.com/questions/223140/prevent-node-from-bein... in which they suggest that in the presave event an Exception is thrown, but I see that they comment that this could cause a break and that an error is generated which would also prevent other events to be thrown. They also suggest that the evaluation should be done in the form, but I need that duplicates are not generated regardless of whether it is being captured from the Drupal interface or if they are being generated programmatically with ECA or Feeds, they also suggest the use of Constraints, but I would like to avoid the use of custom code, so I tried with some modules such as:

Unique field - https://www.drupal.org/project/unique_field
Unique content field validation - https://www.drupal.org/project/unique_content_field_validation
Field Validation - https://www.drupal.org/project/field_validation

The first one indicates that it only works for nodes and does not work when they are created programmatically, the second one although it shows me options to activate it in the Storage fields, when testing it does not work. The third one works when I create the entity from the Drupal interface and apparently also when I do it from Feeds, that is, when I activate it no duplicates were created, but the messages that Feeds shows me do not match those that I defined in the module, anyway, it does not prevent duplicates from being created when it is an ECA model that is creating the entity.

Do you know if there is any way I can accomplish this to avoid duplicates with ECA?

💬 Support request
Status

Active

Version

2.0

Component

Miscellaneous

Created by

🇲🇽Mexico ovilla

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

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

  • 🇩🇪Germany jurgenhaas Gottmadingen
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024