0

I have a module that creates an AJAX Form.

I need to copy some values from the DOM to a form field when (or before) the form is submitted.

In my module JS, I tried this:

(function($, Drupal) {

  Drupal.behaviors.beforeAjaxFormSubmit = {
    attach: function(context, settings) {

      Drupal.Ajax.prototype.beforeSubmit = function(form_values, element, options) {

        console.log('Will alter form here ...')
      };
    }
  };
})(jQuery, Drupal);

The console log is shown but right after comes an AJAX Error:

An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /intervention/create?ajax_form=1
StatusText: OK
ResponseText: TypeError: Argument 1 passed to Drupal\Core\Render\MainContent\AjaxRenderer::renderResponse() must be of the type array, null given, called in /var/www/html/web/core/lib/Drupal/Core/Form/FormAjaxResponseBuilder.php on line 89 in Drupal\Core\Render\MainContent\AjaxRenderer->renderResponse() (line 49 of /var/www/html/web/core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php).
[[Prototype]]: Error at http://d9.localhost:8000/core/misc/ajax.js?v=9.4.2:110:32

Does anyone know what I am doing wrong or if this is not the right approach?

Edit:

(function($, Drupal) {

  Drupal.behaviors.beforeAjaxFormSubmit = {
    attach: function(context, settings) {

      // Add submit handler to form.beforeSend.
      // Update Drupal.Ajax.prototype.beforeSend only once.
      if (typeof Drupal.Ajax !== 'undefined' && typeof Drupal.Ajax.prototype.beforeSubmitInterventionForm === 'undefined') {
        Drupal.Ajax.prototype.beforeSubmitInterventionForm = Drupal.Ajax.prototype.beforeSubmit;
        Drupal.Ajax.prototype.beforeSubmit = function(form_values, element_settings, options) {

          // Things will happen here
          
          return this.beforeSubmitInterventionForm.apply(this, arguments);
        };
      }
    }
  };
})(jQuery, Drupal);

This is what I have now but I am still getting the same AJAX Error.

1 Answer 1

1

You need to call the code of the original beforeSubmit().

I've found this great example in the webform module:

/webform/js/webform.element.managed_file.js

  // Add submit handler to form.beforeSend.
  // Update Drupal.Ajax.prototype.beforeSend only once.
  if (typeof Drupal.Ajax !== 'undefined' && typeof Drupal.Ajax.prototype.beforeSubmitWebformManagedFileAutoUploadOriginal === 'undefined') {
    Drupal.Ajax.prototype.beforeSubmitWebformManagedFileAutoUploadOriginal = Drupal.Ajax.prototype.beforeSubmit;
    Drupal.Ajax.prototype.beforeSubmit = function (form_values, element_settings, options) {
      var $form = this.$form;
      var $element = $(this.element);

      // Determine if the triggering element is within .form-actions.
      var isFormActions = $element
        .closest('.form-actions').length;

      // Determine if the triggering element is within a multiple element.
      var isMultipleUpload = $element
        .parents('.js-form-type-webform-multiple, .js-form-type-webform-custom-composite')
        .find('.js-form-managed-file').length;

      // Determine if the triggering element is not within a
      // managed file element.
      var isManagedUploadButton = $element.parents('.js-form-managed-file').length;

      // Only trigger block submit for .form-actions and multiple element
      // with file upload.
      if ($form.data('webform-auto-file-uploads') > 0 &&
        (isFormActions || (isMultipleUpload && !isManagedUploadButton)) &&
        blockSubmit($form)) {
        this.ajaxing = false;
        return false;
      }
      return this.beforeSubmitWebformManagedFileAutoUploadOriginal.apply(this, arguments);
    };
  }
5
  • Thank you for your answer. What part(s) of the code must be called?
    – MrUpsidown
    Commented Aug 8, 2022 at 9:11
  • The last line with the variable you've saved before overwriting the function.
    – 4uk4
    Commented Aug 8, 2022 at 9:18
  • Right. Please see my edit. Unless I misunderstood you, this is what I have done and I am still getting the same error.
    – MrUpsidown
    Commented Aug 8, 2022 at 9:29
  • 1
    You understood me correctly, I think your code is now OK. However, the effect this has depends on what other code might be in beforeSubmit() in other places, which I don't know. Now you make sure you don't overwrite it.
    – 4uk4
    Commented Aug 8, 2022 at 9:56
  • After further debugging, it appears the AJAX error was unrelated. This works as expected. Thank you!
    – MrUpsidown
    Commented Aug 8, 2022 at 11:34

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.