- Issue created by @joachim
Currently, making a form element update via AJAX requires setting up the #ajax property on the element, and implementing a form AJAX callback. Typically this callback just returns the form element like this:
function myCallback($form, FormStateInterface $form_state) {
return $form['my_element'];
}
That's pointless boilerplate code.
Furthermore, you can only update a single form element with this system. You can't have multiple parts of the form update in response to one element.
Nor can you extend a form by having elements added with hook_form_alter() react to an element that's already an AJAX trigger.
Introduce a declarative AJAX system, similar to how form states work, like this:
$form['controlling_element'] = [
'#type' => 'checkbox',
'#title' => 'Click me',
];
// This container is automatically updated when the user changes the value of
// the controlling_element checkbox.
$form['my_container'] = [
'#type' => 'container',
'#ajax' => [
'updated_by' => [
// Form parents of the controlling element.
['controlling_element'],
],
],
];
Multiple elements can declare that they are updated by one controlling element, and conversely, one element can be updated by more than one controlling element. There is no longer the need for a callback: everything is declared in the form elements.
I've created a proof-of-concept module with working demos here: https://www.drupal.org/project/declarative_form_ajax β
This requires a few tweaks, but those wouldn't be necessary within core.
Create a core MR.
None.
See above.
None.
Active
11.0 π₯
Last updated