0

I'm trying to open a webform in a modal by using OpenModalDialogCommand in an AjaxResponse like this.

  • in buildForm method, I'm specifying AJAX callback like this:
$form['actions']['submit'] = [
  '#type' => 'submit',
  '#value' => $this->t('Send'),
  '#ajax' => [
    'callback' => [$form_state->getFormObject(), 'ajaxSubmitForm'],
    'disable-refocus' => TRUE,
    'progress' => 'none',
  ],
];
  • in ajaxSubmitForm, I'm specifying AjaxResponse like this:
$output['webform'] = [
  '#type' => 'webform',
  '#webform' => 'contact',
  '#default_data' => [],
];
$response->addCommand(new OpenModalDialogCommand(
  'My title',
  $output,
  [
    'width' => '600px',
    'dialogClass' => 'social-dialog--task-webform',
  ]
));
return $response;

This works fine, but the action attribute of the <form> rendered in the modal is set to the path of the page, instead of the webform. Which results in HTML being returned in the AJAX response and the Drupal.AjaxError in the browser console. How can I fix this?

EDIT: I noticed, that views_ui core module defines a SetFormCommand, which allows for custom URL used when views form is displayed in the modal. Isn't something like this also needed for webform forms?

EDIT 2: So I tried to reimplement SetFormCommand for webform, but it's not working. Perhaps I'm limited by my insufficient knowledge of AJAX API in Drupal here.

3
  • I haven't used OpenModalDialogCommand yet but found this link check once .. hope will be helpful.
    – Lokesh1024
    Oct 10, 2022 at 11:31
  • forget to add forum link too .. check solution.
    – Lokesh1024
    Oct 10, 2022 at 12:02
  • Thanks @Lokesh1024, this are indeed helpful resource, but none of them is a webform-specific - which is what I need. Oct 10, 2022 at 15:05

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.