What I want:
Put a button on the website and if the button will be clicked, load a "node add form" from a specific bundle with Ajax. So the user can fill out and save the form. Then the current page should reload as usual and save the new created node.
- What: I've loaded a "node add form" like this example: https://drupal.stackexchange.com/a/199600
- Where: The "node add form" is loaded in a custom controller with an AjaxResponse() and ReplaceCommand(). So if I click a Button with this controller-path, the form is loaded into my website. Everything is working fine so far.
- Problem: But when I'm going to save the loaded form, the page will reload, but no node will be saved and no form submit callback is working.
- Debug: If I'm loading the "node add form" in the controller without AjaxResponse(), it works like a charm. But as soon as the "node add form" is loaded with an AjaxRepsonse, no form submit callback works anymore.
Why is that? The callbacks are in both variants the same, like:
$form['actions']['submit']['#submit'][] = '::submitForm'
$form['actions']['submit']['#submit'][] = '::save'
Comments
Comment #2
handkerchiefComment #3
handkerchiefOk I've discovered that If I click on the submit (save) button of the ajax loaded entity form, the same controller is called again... instead of the entity form submit callbacks. Why?
Comment #4
handkerchiefI figured out the problem:
$form['#action']On both, the normal loaded "node add form" and the ajax loaded "node add form", the action parameter has this value:
"form_action_p_pvdeGsVG5zNF_XLGPTvYSKCf43t8qZYSwcfZl2uzM"This placeholder is replaced with the path from the current route (I guess). So if I load an entity form with AjaxResponse in a controller, the form action path will be the controller path itself. So on submit, the controller will call again instead of the form callbacks.
As a workaround, I can replace the form action placesholder with the hardcoded path, e.g. "/node/add/article", then it works normal. but I'm dealing with the placeholder, which shouldn't be. What would the correct way be?
Comment #14
smustgrave commentedThis came up as the daily BSI target.
First question is do you know if you're still experiencing this? Tried following the summary but not clear what the exacts steps were.
Also we have HTMX now which may serve as a better solution?