I posted this a few weeks ago: Why doesn't form submit callback get called? and with @4k4's help was able to get my custom form opening a modal when the form was submitted (see answer posted there). I had to leave this for a few weeks and when coming back to it, it seemed as though my modal no longer opens.
My form submits and the throbber spins, but the modal never opens. I see this console error:
Object { message: "\nAn AJAX HTTP error occurred.\nHTTP Result Code:
200\nDebugging information follows.\nPath:
/create-mpep-quote?ajax_form=1\nStatusText: OK\nResponseText: \nAdd
payment | Specialty Insurance Agency\nSkip to main content\nToolbar
items\nBack to site\nManage\nAdministration menuToolsIndexFlush all
cachesFlush CSS and JavaScriptFlush plugins cacheFlush render
cacheFlush routing and links cacheFlush static cacheFlush twig
cacheFlush views cacheRebuild theme registryFlush compiler
cacheDevelopmentConfig editorDevel Toolbar SettingsDevel
settingsElement InfoEntity infoExecute PHP CodeRebuild menuReinstall
modulesSession viewerState editorTheme registryRun cronRun
updatesLogoutContentSIA (Policies)Extra policiesParagraphsPolicies up
for renewalAdd contentAccident InvestigationArticleBasic
pageComponentEvent/VenueKnowledge BaseLanding PageMPEP ApplicationMPEP
QuotePerformer Certificate RequestPerformer Insurance
ApplicationPolicyRequest Account ChangeSIA SlideshowSpecial
EventTeamTestimonialsUpgradeVendor…", name: "AjaxError", stack:
"@http://mysite/core/misc/ajax.js?v=9.4.2:110:32\n@http://mysite/core/misc/ajax.js?v=9.4.2:676:3\n"
}
Not of too much use but it does look like some entries from the Admin Toolbar in the ajax response. I then recalled I was testing this a few weeks ago as anonymous (which won't have the toolbar enabled). Trying again as anonymous (or any non-admin account) and the modal does open correctly.
The modal code is pretty straight forward:
Controller to load a Node create form:
public function createQuote() {
$node = Node::create(['type' => 'mpep_quote']);
$form = \Drupal::service('entity.form_builder')->getForm($node);
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
return $form;
}
A form alter before-hand to modify the Node add form:
$form['actions']['submit'] = array_merge($form['actions']['submit'], [
'#value' => t('Get quote'),
'#ajax' => [
'callback' => 'openQuoteModal',
'event' => 'click',
]
]);
And the function to open the modal:
function openQuoteModal($form, $form_state) {
$response = new AjaxResponse();
$cart = "Hello world!!";
// Add an AJAX command to open a modal dialog with the form as the content.
$response->addCommand(new OpenModalDialogCommand('Your quote', $cart, ['width' => '1000']));
return $response;
}
Is there something that needs to be done with the modal code to prevent the toolbar from messing this up? Possibly as simple as a $response call to remove the toolbar?