Boolean field with Radio button or Checkbox widget does not fill in textfield/select

Created on 26 September 2017, almost 7 years ago
Updated 4 March 2024, 4 months ago

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. Evaluate the patch in #10, which seems to take a different, but not necessarily incorrect, approach.
  2. Review and feedback
  3. RTBC and feedback
  4. 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.

πŸ› Bug report
Status

Postponed: needs info

Version

4.0

Component

Javascript

Created by

πŸ‡ͺπŸ‡ΈSpain manuel.adan 🌌

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡©πŸ‡ͺGermany diqidoq Berlin | Hamburg | New York | London | Paris

    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?

Production build 0.69.0 2024