I have a view listing some content and I want to have an "Add new" button, which should load the node form via ajax (either modal or into a div). After I save the form, it should still stay on the page. Updating the view would also be nice, but for the first thing I'm trying to load the node form via ajax. How can I do that?


The most basic version would be to add 'use-ajax' to the "Add new" button link pointing at a controller which returns an ajax command replacing the div. See https://www.drupal.org/docs/drupal-apis/ajax-api/basic-concepts#s-trigger-ajax-request-with-a-link.

Modals use also the 'use-ajax' class but need additional 'data-dialog-*' attributes. Then you can load normal HTML content in a dialog box, you don't need an Ajax controller in this case. See https://www.drupal.org/docs/drupal-apis/ajax-api/ajax-dialog-boxes

In a submit handler of the form you have to make a redirect to a URL you have setup to display the updated view together with the form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.