Problem/Motivation

It is possible to create a dependency such that a checkbox or radiobutton target field is checked when the control field meets some condition, but this doesn't work because the #states array is set on the checkbox or radiobutton wrapper.

Steps to reproduce

  1. Download and install Drupal core with the Standard install profile, version 8.3.4 or 8.3.x (I tested on commit dcd645bda8).
  2. Download and install Conditional Fields, version 8.x-1.x-dev (I tested on commit 8060870).
  3. Modify the Basic page content type, or create a new content type (I called mine "Conditional fields test")
  4. Add a Boolean field named "Boolean should change" to the content type. I did not make it a required field.
  5. Add a List (text) field named "Triggering field" to the content type. Give it two options, "Option 1" and "Option 2". I made it a required field and set the default to "Option 1".
  6. Go to the "Manage dependencies" tab. Add a new dependency. Set Target field = "Boolean should change", Control field = "Triggering field", The target field is "Checked" when the control filed "has value...". Click "Add dependency".
  7. In the "Insert value from widget" fieldset, set "Triggering field" to "Option 2". Leave all other controls at their default state. Click "Save settings".
  8. Go to the node/add page for the content type you just created. Set "Triggering field" to "Option 2". Note that "Boolean should change" does not change state (expected: it gets checked). Set "Triggering field" to "Option 1". Note that "Boolean should change" does not change state (expected: it gets unchecked).
  9. If you can, set a breakpoint on the last line of conditional_fields.api.inc's conditional_fields_form_after_build() function (i.e.: return $form).
  10. Flush caches and reload the node/add page for the content type you just created. You should hit the breakpoint. Inspect $form['field_boolean_should_change']. Note that $form['field_boolean_should_change']['#type'] === 'container'. Note that $form['field_boolean_should_change']['#states']['checked']['[name="field_triggering_field"]'] === ['value' => 'Option 2'].
  11. Resume execution so the page loads.
  12. Inspect the loaded page DOM. Look for the HTML div tag with the ID attribute set to edit-field-boolean-should-change-wrapper. Select "Option 2" from the triggering field. Note that the aforementioned HTML div tag gains the attribute checked="checked".

Proposed resolution

In conditional_fields_form_after_build(), if the state is "checked" or "not checked", and the dependent form field is a container (i.e.: the form field wrapper around a checkbox or radio button), and there is a checkbox or radio button deeper in the array, we actually need to use the (deeper) checkbox or radio button because it is not possible to set a checked state on a container.

Remaining tasks

  1. Write a patch
  2. Review and feedback
  3. RTBC and feedback
  4. Commit

User interface changes

No changes.

API changes

No changes.

Data model changes

No changes.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mparker17 created an issue. See original summary.

mparker17’s picture

Assigned: mparker17 » Unassigned
Issue summary: View changes
Status: Active » Needs review
FileSize
1.66 KB

Patch attached. Feedback welcome!

mparker17’s picture

The patch in #2 applies to 8.x-1.x-dev but does not apply to the last release of the module (8.x-1.0-alpha2) (because the patch context at the very bottom does not match — it was changed in #2886483: Dependency on select list values not exploded), so I've attached a separate patch for the convenience of users on 8.x-1.0-alpha2.

Reviewers: please review the patch from #2 and test against 8.x-1.x-dev.

colan’s picture

Status: Needs review » Needs work

#2 applies cleanly, but can we get #2926132: OOify contents of conditional_fields.api.inc and then delete it done first, before making it worse?

colan’s picture

Status: Needs work » Postponed

Actually, let's postpone this until that gets done.

alabandit’s picture

#3 does not seem to apply to 8.x-1.0-alpha4

yang_yi_cn’s picture

Priority: Normal » Major
Status: Postponed » Reviewed & tested by the community

The patch still applies to the dev version as of now. And it works. I suggest to merge this patch first, and do the other refactor ticket later, instead of having the other refactor ticket blocking this (for over 1 year).

rfulcher’s picture

I have applied this patch and it does seem to work however I have an issues and not sure if it is just a configuration issue or what. I have two checkboxes that I am setting up a dependency for. If A is checked then B is available to be checked. The condition for enabled works great. Now if A is unchecked then I want to uncheck B and disable B from being checked. B is only an option that is available when A is checked. At the moment I can only get A and B to check and uncheck at the same time. Specifically when A is checked I want B to be available to be checked, and if B is checked and A is unchecked I want B to be unchecked. At the moment If I check A, B is automatically checked(Not Desired), If I uncheck A, B is automatically unchecked (Desired).

I went through all the configuration and every rule either ended in the check/uncheck not working or it always checked/unchecked both at the same time.

As a side note the enable/disable of B based on controlling field A works great.

Maybe there is a way it should be configured and I am just not getting it.

Thanks

colan’s picture

Status: Reviewed & tested by the community » Needs work

Fair enough re: #7, but it no longer applies so will need to be re-rolled. That issue is a beta blocker anyway.

#8: If you can confirm that it's a problem with the patch here, please let us know. Otherwise, open a separate support ticket. Thanks!

paulmckibben’s picture

Status: Needs work » Needs review
FileSize
1.4 KB

I've rerolled #2 due to the refactor of conditional_fields.api.inc into src/ConditionalFieldsFormHelper.php. It works for me locally.

agiraud’s picture

I've rerolled the patch for the latest version.

alfattal’s picture

I've applied the patch in #11 and it worked. However, I'm having unexpected behavior. I have two checkbox fields set for Taxonomy terms where the dependency is set to make the target field (A) checked when the the controller field (B) is checked. The issue is that for some terms, field (A) is checked and field (B) is not, but after applying this dependency, field (A) is automatically unchecked whenever field (B) is unchecked. I've tried to add another dependency to set (A) as unchanged when (B) is unchecked, but it didn't do anything to prevent that behavior which makes such dependency useless.

maskedjellybean’s picture

The patch in #11 applies cleanly to the dev version and fixes my issue.

hmendes’s picture

Version: 8.x-1.x-dev » 4.x-dev
FileSize
1.27 KB

Re-rolling patch from #11.
Please review.

hmendes’s picture

It wasn't working when we had multiples conditions on the field.

jessicacs’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
27.89 KB
28.15 KB

I made the test with the patch #15 and it's worked.

  • colan committed d955d61 on 4.x authored by mparker17
    Issue #2891276 by hmendes, mparker17, agiraud, paulmckibben, jessicacs:...
colan’s picture

Status: Reviewed & tested by the community » Fixed

Thanks!

siliconmeadow’s picture

Issue summary: View changes

...just ticking off the to-do list in the description.

Good work - looking forward to seeing this in the next release and am about to try applying the patch to alpha-1 in the meantime.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.