My server sometimes gets overloaded, and sometimes people press the "Save" button on an entity creation page several times, which results in multiple versions of the same entity being created.
So, I want to prevent users from saving the same entity (an entity that has the exact same field content as the immediately previous entity saved by the user).
One fix is to get a better server. I'm working on that.
Still, I would like to programmatically prevent users from saving the exact same entity twice in a row. In my specific use case, this entity is a new private message from the Private Message module.
Is there a "best practice" way to do this?
Here is what I have considered.
- On
hook_entity_presave()
, query the database and check if the last entity saved by the user had the same values. - Add an entity reference to the user profile for the last saved entity, and update the user profile entity reference field with the value of the new entity every time it is saved. When saving a new entity, check it against the values of the entity reference entity.