Problem/Motivation
The event handler listening for state:disabled never gets triggered.
Steps to reproduce
Create a simple form with a checkbox and a Chosen select whose enabled states depends on the checkbox.
$form['enabler'] = [
'#type' => 'checkbox',
'#title' => 'Enable',
];
$form['selecter'] = [
'#type' => 'select',
'#title' => 'Select',
'#chosen' => TRUE,
'#options' => [
'foo' => 'Foo',
],
'#empty_option' => '- Empty -',
'#states' => [
'enabled' => [
':input[name="enabler"]' => ['checked' => TRUE],
],
],
];
While the actual select element gets its disabled attribute toggled by the checkbox, the Chosen select starts out and always remains enabled.
Proposed resolution
The states events are triggered via jQuery (both in 10.5.x and in 11.x). jQuery only calls jQuery event handlers and native event handlers specified through the onevent
attributes (both in 3.7.1 and 4.0.0-rc.1). As this module's event handler is added through the native addEventHandler
method as of 5.0.2, it never gets called by jQuery.
My proposed solution is to add back to jQuery dependency and listen for state:disabled with a jQuery event listener.
Remaining tasks
Maybe there is a way to dynamically detect when #states or jQuery is used, and only add the event listener in that case.
User interface changes
None.
API changes
None.
Data model changes
None.