Support dynamic forms using HTMX

Created on 10 July 2025, 2 days ago

Problem/Motivation

In general, \Drupal\Core\Form\FormBuilder is aware of ajax api requests and needs to be adjusted so that it is similarly aware of HTMX requests.

A thorny problem with dynamic elements was encountered in πŸ“Œ [POC] Implementing some components of the Ajax system using HTMX Active .
The form used as our discovery and development context for this POC, \Drupal\config\Form\ConfigSingleExportForm, has <select> elements with values that dynamically change based on user input.

With form builder as it is, if the form route is called with user input that is not a submit, the form rebuilds with a new build ID and fails to validate because the options get reset to their base case, and the user input no longer matches the available options.

Steps to reproduce

Once we have dependent issues committed, I'll update ConfigSingleExportForm without changing FormBuilder so others can see the issue and explore solutions.

Proposed resolution

In the POC I solved the problem like this.

if ($this->isHtmxRequest()) {
        // Restore the build id that was sent with the request. It will be used
        // after the rebuild to cache the rebuilt form.
        $form_state->addRebuildInfo('copy', ['#build_id' => TRUE]);
        $input = $form_state->getUserInput();
        $form['#build_id'] = $input['form_build_id'];
      }

Remaining tasks

Refactor FormBuilder
Add appropriate tests

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component

ajax system

Created by

πŸ‡ΊπŸ‡ΈUnited States fathershawn New York

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024