1

Currently if a form is submitted via ajax is will show the throbber and "please wait" message(which can be configured on the ajaxified button that submits the form.

I would like to perform some additional actions before and after the form is submitted via ajax so I am looking for events that are being triggered and on which elements they are being triggered.

2
  • javascript events or drupal events?
    – Eyal
    Dec 1 '15 at 22:54
  • JS. I have been going through the javascript file in the core and to me it looks like the only way to do this is to watch for submit event on the form and that's it really, nothing else there can be done since the jquery form script doesn't do anything. The only "indicator" of a form being submitted is "ajaxying" on the ajax object but that is not catchable.
    – user21641
    Dec 2 '15 at 7:23
7
+100

When you look in ajax.js you find several prototype functions, that can be used like hooks in drupal:

core/misc/ajax.js:

 /**
   * Modify form values prior to form submission.
   *
   * @param {object} form_values
   * @param {HTMLElement} element
   * @param {object} options
   */
  Drupal.Ajax.prototype.beforeSubmit = function (form_values, element, options) {
    // This function is left empty to make it simple to override for modules
    // that wish to add functionality here.
  };

You can override this prototype-functions in your own js-file:

(function($) {
   var ajaxBeforeSubmitOriginal = Drupal.Ajax.prototype.beforeSubmit;
   Drupal.Ajax.prototype.beforeSubmit = function (form_values, element_settings, options) {

      // your code  

      // call original function since it can be already overridden 
      ajaxBeforeSubmitOriginal.call(this, form_values, element, options);
   }
})(jQuery);

This is the most basic version. More info: https://www.drupal.org/node/1372840

2
  • Yes, I thought about that too but I wanted some sort of non-intrusive approach but it looks like that at this time it is not possible so I am accepting this answer.
    – user21641
    Dec 2 '15 at 14:03
  • Yes this does the job well. Cheers. Apr 2 '17 at 19:23
1

I think the most simple way you can handle this(without worrying about Drupal at all) is by using Jquery methods described at http://api.jquery.com/category/ajax/global-ajax-event-handlers (more examples at https://stackoverflow.com/questions/5051276/how-to-know-which-element-in-body-triggered-ajax-request-in-jquery)

I'm not sure if Drupal 8 has some particular code for that, but with pure JS you'll be able, for sure.

3
  • Why downvoting?
    – pedrorocha
    Sep 22 '15 at 0:12
  • 2
    Because you didn't post an answer, but merely a link to answer, and that is considered very low quality here.
    – Mołot
    Dec 1 '15 at 9:19
  • 1
    I think that rule Discourage users to answer instead you could edit the answer or ask for better answer to encourage users to post good answer (considered kind of punishment) Dec 1 '15 at 12:25
0

You could use validateForm to trigger before your form is submit, you just need to get the triggered element. For the "after" part, you could check if your call/ajax code has worked (return a bool) and then do something.

I'm not to familiar with drupal 8 though but that the way I'll do it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy