I'm trying to render a Drupal 8 modal dialog form through JavaScript, so it can appear on multiple pages across my site. In the controller class, I create an AjaxResponse object with an openModalDialogCommand for the modal form. I want to render the modal dialog through JavaScript, so that I can control in which pages it displays.

$(document).ready(function() {
  $('body').append('<div id="ao-interstitial-modal"></div>');

This the JavaScript code I write so far, but it renders a textarea in the body tag with the AJAX command object as text inside it.

What do I need to do to render the form as a dialog popup?

  • 1
    You shouldn't need any JavaScript for that. But please edit your question and try to explain exactly what you are trying to do and why? What form? Where on the page exactly? Do you have any sample use case?
    – leymannx
    Commented Aug 16, 2018 at 19:56

1 Answer 1


Loading a form via ajax is no different then loading any other page with ajax. The quick answer is that you should create a route to display the form and then call that route via ajax but for a detailed description you should take a look here: https://www.drupal.org/node/2046693

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.