1

I have a Drupal 8 form I want to show via JavaScript code to make it appear as a popup across multiple pages. The form is rendered through an AJAX response in a controller. I can see the response using $.ajax(). How can I invoke the AJAX response to display on the page?

The JavaScript code is the following one.

$(document).ready(function() {
  $.ajax({
    url: '/aao_additions/modal_form',
    // Data parameter is the ajax response data for the intersitial popup.
    success: function(data, status, jqxhr) {
      console.log(data);
      console.log(status);
      var popup = Drupal.ajax({
        url: "",
        base: false,
        element: false,
        progress: false,
      });
      // Simulate an AJAX response having arrived, and let the Ajax system handle it.
      popup.success(data, "success");
    },
    // Error function for if the modal form cannot be loaded.
    error: function(xhr, desc, err) {
      console.log(xhr);
      console.log("Desc: " + desc + "\nErr:" + err);
    },
  });
});

I receive an error on Drupal.ajax. I think it is preventing the response rendering.

4
  • 1
    Welcome to Drupal Answers! Please update your question to include a properly documented code snippet of what you've done so far.
    – leymannx
    Commented Aug 16, 2018 at 15:23
  • I've added my javascript where I'm trying to invoke the ajax. I'm sort of tricking the ajax call to run with a success call on Drupal.ajax(). This worked for my route path for the form but it throws an error on other pages.
    – Trey D
    Commented Aug 16, 2018 at 15:37
  • At a very high level, I think you want a link that has a class of "use-ajax" on it whos callback hits your controller, the controller returns an AjaxResponse to trigger the modal. I don't understand your JS above, because that code will fire AJAX every single page. At the same time, The JS will need to conform to be a Drupal behavior so it is compatible. drupal.org/docs/8/api/javascript-api/javascript-api-overview
    – Kevin
    Commented Aug 17, 2018 at 13:28
  • I do have a link that calls my controller and this is the only page where the JavaScript call works. I want to call the controller automatically on other pages through my JavaScript. Is there a way to do this?
    – Trey D
    Commented Aug 17, 2018 at 14:21

0

Your Answer

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

Browse other questions tagged or ask your own question.