Problem/Motivation
If you create a condition where the controlling field is a boolean field with a checkbox or radio button widget, and the action is to fill a value in a textbox, nothing happens.
Steps to reproduce
- Download
Drupal 8.9.0 →
and
clone Conditional Fields branch 8.x-1.x →
(when I wrote this, the latest commit was
9c9930e
)
- Install Drupal using the Standard install profile. When that is done, install Conditional Fields.
- Go to
/admin/structure/types/manage/article/fields
, add a Boolean field named "Is external author" (machine name field_external_author
). Use the default configuration values.
- Go to
/admin/structure/types/manage/article/fields
, add a Text (plain) field named "Author" (machine name field_author
). Use the default configuration values.
- Go to
/admin/structure/types/manage/article/form-display
, set the Widget for "Is external author" to Single on/off checkbox
and click Save.
- Go to
/admin/structure/types/manage/article/conditionals
, and add a condition/dependency as follows:
- Target field =
field_author
- Controlled by =
field_external_author
- The target field is =
filled with a value
- when the control field
has value...
- Click
Add dependency
- Condition =
Value
- Values input mode =
Insert value from widget...
- Insert value from widget:
- Is external author =
On
- Interaction with other dependencies =
AND
- Edit context settings:
- Form state =
Visible
- Fill field with a value effect option: value =
Author doesn't work here
- Restore previous value when untriggered = (checked)
- Advanced edit context settings:
- (Leave "Custom jQuery selector for control field" empty)
- Click "Save settings"
- Go to
/node/add/article
. Change the "Is external author" value between N/A, "Off", and "On".
- Actual behavior: "Author" field does not change.
- Expected behavior: "Author" field changes to "Author doesn't work here" when "Is external author" is set to "On". "Author" field returns to its previous value (i.e.: empty) if "Is external author" is set to "N/A" or "Off".
Note that changing the widget for "Is external author" to a checkbox and adjusting the condition/dependency accordingly also does not produce the expected behavior.
Analysis
In @mparker17's test run where the controlling field was a checkbox, the if statement on line 65 of the following snippet from conditional_fields.js
always evaluated to false both when the checkbox was checked and when it was unchecked, so the code inside it was never run. Specifically, e.effect
doesn't exist, which I assume is the root issue.
/* 47 */ $(document)
/* 57 */ // Empty/Filled.
/* 58 */ .bind('state:empty', function(e) {
/* 59 */ if (e.trigger) {
/* 60 */ var fields = $(e.target).find('input, select, textarea');
/* 61 */ fields.each(function() {
/* 62 */ if (typeof $(this).data('conditionalFieldsSavedValue') === 'undefined') {
/* 63 */ $(this).data('conditionalFieldsSavedValue', $(this).val());
/* 64 */ }
/* 65 */ if (e.effect && e.effect.reset) {
/* 66 */ if (e.value) {
/* 67 */ $(this).val(e.effect.value);
/* 68 */ }
/* 69 */ else if ($(this).data('conditionalFieldsSavedValue')) {
/* 70 */ $(this).val($(this).data('conditionalFieldsSavedValue'));
/* 71 */ }
/* 72 */ }
/* 73 */ })
/* 74 */ }
/* 75 */ })
Proposed resolution
To be determined
Remaining tasks
- Evaluate the patch in #10, which seems to take a different, but not necessarily incorrect, approach.
- Review and feedback
- RTBC and feedback
- Commit and release
User interface changes
To be determined; likely none.
API changes
To be determined; likely none.
Data model changes
To be determined; likely none.
Release notes snippet
To be determined.
Original report by manuel.adan
A simple scenario: boolean field (radio widget) controlling a play text field. Target field does not changes at all, no JS errors on console. Other actions, like disable/enable or hide/show target works well.