Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Rex_Soriano on
I'm working on my first Drupal 8 site and I'm having trouble with the php ajax forms when I place them into twig templates.
For example, I'll access the form using the path and the ajax events will work.
When I access the form rendered on a twig template the ajax events won't fire.
To get the form in the TWIG template, I'm using a preproccess_html hook and loading the form with the drupal form builder.
My question is how do I get the AJAX events to fire when rendering my form in a twig template? Do I need to create a twig extension?
Comments
Missing Drupal Settings.
When I compare the source of the two forms it looks like the one generated from the twig template is not generating the #AJAX routes in the Javascript drupal settings array. Is there a step I'm missing when generating the form? I'm using the getForm method on the form builder... is there another way to generate forms that guarantees form elements with #ajax keys get added to the drupal settings array?
Figured it out.
Created form in the pre-process page hook and the ajax calls work fine.
My guess is that the pre-process html hook doesn't register the ajax calls with the drupal settings javascript variable.