I see a lot of examples of modal forms. But in all these example the modal is started from a current form. There is always a link-button involved. I would like to start a form as a modal from the controller by using:

$form = \Drupal::formBuilder()->getForm($formId); return $form;

Can someone give me an example of which extra code is needed to show the form ($formId) as a modal??

  • MediaCurrent just posted about this. Everything you should need is here: mediacurrent.com/blog/… – Kevin Mar 15 '17 at 16:53
  • You could, but you need some sort of trigger to hit a callback. That callback will return an AjaxResponse instead of the standard response from a controller. – Kevin Mar 15 '17 at 17:05

I did an example of this a while ago. Basically I defined a route, and that controller returned an AjaxResponse:

  path: '/mymodule/growl'
    _controller: '\Drupal\mymodule\Controller\MyModuleController::growl'
    _permission: 'access content'

Basic controller:

namespace Drupal\mymodule\Controller;

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Controller\ControllerBase;
use Drupal\igrowl\Ajax\GrowlCommand;

class MyModuleController extends ControllerBase {
  public function growl() {
    $options = GrowlCommand::defaultOptions();
    $options['message'] = 'This is a message.';
    $options['title'] = 'Hello';
    $options['icon'] = 'feather-check';
    $options['type'] = 'success';

    $response = new AjaxResponse();
    $response->addCommand(new GrowlCommand($options));
    return $response;

Any link hitting that route fired that Command. So, anytime someone clicks a link going to your defined route, it should create a modal.

You basically want to return a form (use the form builder service provided by ControllerBase instead of the static container) and at a basic level, use OpenModalDialogCommand (or supply your own modal js with custom Command). AjaxResponse will take care of the rest.

| improve this answer | |
  • Installed iGrowl but did not get it working. I get a pop-up with just a list of the options – Bart van Leeuwen Mar 16 '17 at 21:46
  • I found the iGrowl module and installed it, but the module does not pace up with the changed structure in iGrowl.zip. I also do not know how to add the libraries and which, while they are already loaded in the Drupal module itself. – Bart van Leeuwen Mar 16 '17 at 21:48
  • Any way we can trigger a modal from twig? I have a controller that returns the AJAX response. – Prerit Mohan Mar 13 '19 at 15:10

I solved my own problem: in stead of a modal I use an overlay which reports a message.

My question seemed a bit theoretical, but in the Mediator Design Pattern I use, I want to solve my problems generically. In the main program I can decode when a form takes long time, before I render the form I attach extra JS which is coupled to the submit button.

| improve this answer | |

You can force any link to open in a modal dialog. Just add the class use-ajax and the attribute data-dialog="modal" to a link.

<a href="/node" class="use-ajax" data-dialog="modal">node list</a>

P.S. Drupal uses JavaScript to apply this behavior, so make sure set the class and attribute in the server side (template/preproces/pre-render...)

| improve this answer | |

Your Answer

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

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