How can I programatically prevent users from saving the same entity twice in a row? 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.
I find this to be a UX issue. 1 thing I would recommend is to allow certain high-traffic forms on your site to show a throbbing/progress/submit indicator upon click.

I believe you can do this using the Form API by overridding the default submit element on your site forms as needed. This is explained some in this related SO post with links to documentation:
https://stackoverflow.com/questions/6100970/how-do-you-remove-the-throbber-from-drupal-7-ajax-link