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 namefield_external_author
). Use the default configuration values. - Go to
/admin/structure/types/manage/article/fields
, add a Text (plain) field named "Author" (machine namefield_author
). Use the default configuration values. - Go to
/admin/structure/types/manage/article/form-display
, set the Widget for "Is external author" toSingle 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
- Is external author =
- 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)
- Form state =
- Click "Save settings"
- Target field =
- 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
Update the issue summary to include steps to reproduce and an analysis of the problem- 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.
Comment | File | Size | Author |
---|---|---|---|
#10 | field-js-selectors-updated-with-inputs-2911857-10.patch | 1.35 KB | labboy0276 |
|
Comments
Comment #2
jazzfiction CreditAttribution: jazzfiction as a volunteer commentedI'm also having this issue where I want to fill out a text field, depending on if a checkbox is checked or not. Right now it isn't working. If I change the behavior to the checkbox hiding/showing the text field, it works.
Comment #3
Rainer F. Gottlieb CreditAttribution: Rainer F. Gottlieb commentedThis helped us > I set "OR" for "Interaction with other dependencies" see here https://www.drupal.org/project/conditional_fields/issues/2907313 but I installed this patch before: https://www.drupal.org/project/conditional_fields/issues/2873350 don't know if this is necessary I tried > Values input mode > set of values > then input of key of the list, but doesnt work, afterwards the I set "OR" for "Interaction with other dependencies"- hint works, but for that I changed back to "insert value from widget".
Comment #4
hawkeye.twolfThis doesn't seem to be solved by the other issue. Re-opening this one. Tested with latest dev branch, and with the patch in #2897468: Notice: Undefined variable: select_states in Drupal\conditional_fields\Plugin\conditional_fields\handler\Select->statesHandler() .
Comment #5
scott.whittaker CreditAttribution: scott.whittaker as a volunteer commentedDoesn't work setting the value of one Select based on the value of another either.
Comment #6
PrineShazar CreditAttribution: PrineShazar as a volunteer commentedHi all, attaching patch, ive not fully tested this (apologies) but ive got this working by updating the JQuery name attribute to use ':input' when building selectors, and pass the expected js object when triggering the conditional field js event.
Comment #7
PrineShazar CreditAttribution: PrineShazar as a volunteer commentedSorry, incorrectly tagged as "Reviewed by the community", this is incorrect, changing this to 'Needs Review" by the community.
Comment #8
dqdWe are on alpha 4 and 1.x-dev
if we want the patch to go in we should reroll against 1.x-dev which has a newer release date then alpha-4...
Comment #9
PrineShazar CreditAttribution: PrineShazar as a volunteer commentedRerolling patch against 1.x-dev.
Comment #10
labboy0276 CreditAttribution: labboy0276 at Tandem commentedI rerolled this against alpha5 / latest dev as of March 12, 2019.
Comment #11
very_random_man CreditAttribution: very_random_man commentedThis is also a problem for the D7 version. The module changes in the patch fix it in some (maybe most?) cases but it still doesn't work for detecting changes to autocomplete field values.
Comment #12
SerShevchykI tested with 8.7.1 and it still not work well. I tried to add target text field controlled by checkbox and another case target text field controlled by checkbox select, both didn't work with the patch
Comment #13
mparker17Updated the issue summary.
Comment #14
mparker17Further refine issue summary and title.
Comment #15
mparker17Updated issue title again - the original report and #3 all mention that we're filling in a value for a textfield; while #5 mentions selecting an option in a select list
Comment #16
dqdThanks for the report, all the efforts and hard work in here. Since EOL Drupal 8/9 we need thoughts/reports if this issue is still needed to be fixed/relevant for 4.x dev and above?