Score:-2

How to open a form in modal from controller while redirect

cn flag

I can open a form in modal when click on a link (and there are lot of examples in the net as well).

What I like to do: When some one go to a url in browser or redirect to a url, it will open a dialog/modal form.

-I have defined a controller for a url -In the controller function I placed the code like this:

  $response = new AjaxResponse();
  $title = $this->t('Confirmation');
  $form = $this->formBuilder->getForm('Drupal\example\Form\MyModalForm', $entity);
  $response->addCommand(new OpenModalDialogCommand($title, $form, ['width' => '800']));

But its not opening the Modal Form, rather showing json code in a textarea. Surely dialog box library might be missing. So I added the library:

    $form['#attached']['library'][] = 'core/drupal.dialog.ajax';

in the form, in the page preprocess, etc. But none is working.

It is showing json code like this (example)

[{"command":"openDialog","selector":"#drupal-modal","settings":null,"data":"Something to show","dialogOptions":{"width":"800","modal":true,"title":"Confirmation"}}]

Can anyone help me, on how to open the dialog/modal when visiting the url?

4uk4 avatar
cn flag
You need client-side javascript to process the ajax commands. See for example https://drupal.stackexchange.com/questions/299270/how-do-i-display-a-modal-dialog-on-page-load-with-working-title-and-dialog-opti
Score:0
cn flag

The approach suggested by @4k4 might be correct, I did not test How do I display a modal dialog on page load, with working title and dialog options, using OpenModalDialogCommand?

What I did: Displayed a link and using JavaScript on load trigger the link, which eventually open the modal dialog.

This is not a good solution for me.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.