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

  1. Download Drupal 8.9.0 and clone Conditional Fields branch 8.x-1.x (when I wrote this, the latest commit was 9c9930e)
  2. Install Drupal using the Standard install profile. When that is done, install Conditional Fields.
  3. 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.
  4. Go to /admin/structure/types/manage/article/fields, add a Text (plain) field named "Author" (machine name field_author). Use the default configuration values.
  5. Go to /admin/structure/types/manage/article/form-display, set the Widget for "Is external author" to Single on/off checkbox and click Save.
  6. Go to /admin/structure/types/manage/article/conditionals, and add a condition/dependency as follows:
    1. Target field = field_author
    2. Controlled by = field_external_author
    3. The target field is = filled with a value
    4. when the control field has value...
    5. Click Add dependency
    6. Condition = Value
    7. Values input mode = Insert value from widget...
    8. Insert value from widget:
      1. Is external author = On
    9. Interaction with other dependencies = AND
    10. Edit context settings:
      1. Form state = Visible
      2. Fill field with a value effect option: value = Author doesn't work here
      3. Restore previous value when untriggered = (checked)
      4. Advanced edit context settings:
        1. (Leave "Custom jQuery selector for control field" empty)
    11. Click "Save settings"
  7. 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

  1. Update the issue summary to include steps to reproduce and an analysis of the problem
  2. Evaluate the patch in #10, which seems to take a different, but not necessarily incorrect, approach.
  3. Review and feedback
  4. RTBC and feedback
  5. 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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

manuel.adan created an issue. See original summary.

jazzfiction’s picture

I'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.

Rainer F. Gottlieb’s picture

Status: Active » Closed (duplicate)

This 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".

hawkeye.twolf’s picture

Status: Closed (duplicate) » Active

This 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() .

scott.whittaker’s picture

Doesn't work setting the value of one Select based on the value of another either.

PrineShazar’s picture

Component: Code » Javascript
Status: Active » Reviewed & tested by the community
FileSize
1.99 KB

Hi 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.

PrineShazar’s picture

Component: Javascript » Code
Status: Reviewed & tested by the community » Needs review

Sorry, incorrectly tagged as "Reviewed by the community", this is incorrect, changing this to 'Needs Review" by the community.

dqd’s picture

Version: 8.x-1.0-alpha2 » 8.x-1.x-dev

#4: Tested with latest dev branch

We 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...

PrineShazar’s picture

labboy0276’s picture

I rerolled this against alpha5 / latest dev as of March 12, 2019.

very_random_man’s picture

This 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.

SerShevchyk’s picture

Priority: Normal » Major
Status: Needs review » Needs work

I 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

mparker17’s picture

Title: Filled with value doesn't work » Boolean field with Radio button widget does not hide/show text field
Component: Code » Javascript
Issue summary: View changes

Updated the issue summary.

mparker17’s picture

Title: Boolean field with Radio button widget does not hide/show text field » Boolean field with Radio button or Checkbox widget does not hide/show text field
Issue summary: View changes

Further refine issue summary and title.

mparker17’s picture

Title: Boolean field with Radio button or Checkbox widget does not hide/show text field » Boolean field with Radio button or Checkbox widget does not fill in textfield/select

Updated 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

dqd’s picture

Version: 8.x-1.x-dev » 4.x-dev
Priority: Major » Normal
Status: Needs work » Postponed (maintainer needs more info)

Thanks 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?