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.

  1. What: I've loaded a "node add form" like this example: https://drupal.stackexchange.com/a/199600
  2. 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.
  3. 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.
  4. 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

handkerchief created an issue. See original summary.

handkerchief’s picture

Issue summary: View changes
handkerchief’s picture

Ok 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?

handkerchief’s picture

I 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?

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

smustgrave’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: +Bug Smash Initiative, +Needs issue summary update, +Needs steps to reproduce

This 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?