5

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

3 Answers 3

3

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

1
2

You can use jQuery to change the save button to a loading gif when the save button is clicked. Thus, preventing any more clicks.

(function($) {
  $('.form-submit').click(function() {
     $('.form-submit').html('<img src="https://media.giphy.com/media/TtZqlvHid7BjW/giphy.gif">');
  });
})(jQuery);

JS Fiddle Example

When you edit nodes it uses the admin theme (seven), so you need to add the jQuery to the admin theme (seven).

Now I don't know if private message uses the admin theme. So you will have to check that. My guess it that it does, but if it doesn't, then you add the jQuery to your theme rather than the admin theme (seven)

Note: The default admin theme for Drupal 7 is "Seven", if you have changed your admin theme, then just edit that theme instead.

1

This seems to be a classic question in the drupal world, did you search already for similar posts?

Here is what my research provided:

This is a two-fold issue, you can start by working on the Client-Side, but to avoid duplicates, you should ultimately work on the Server-Side. Here are what I found to be the "best practice" ways to go about this problem.

Client-Side:

Client-side solution: https://www.drupal.org/project/hide_submit

But what if the javascript is not there?

Server-Side:

Closer to the direction you started out with:

https://drupal.stackexchange.com/a/152181/57183

I would apply both approaches, starting with the "Server-Side" approach.

Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.