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
- Download and install Drupal core with the Standard install profile, version 8.3.4 or 8.3.x (I tested on commit
dcd645bda8
). - Download and install Conditional Fields, version 8.x-1.x-dev (I tested on commit
8060870
). - Modify the Basic page content type, or create a new content type (I called mine "Conditional fields test")
- Add a Boolean field named "Boolean should change" to the content type. I did not make it a required field.
- 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".
- 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".
- In the "Insert value from widget" fieldset, set "Triggering field" to "Option 2". Leave all other controls at their default state. Click "Save settings".
- 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).
- If you can, set a breakpoint on the last line of
conditional_fields.api.inc
'sconditional_fields_form_after_build()
function (i.e.:return $form
). - 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']
. - Resume execution so the page loads.
- 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 attributechecked="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
Write a patchReview and feedbackRTBC and feedbackCommit
User interface changes
No changes.
API changes
No changes.
Data model changes
No changes.
Comment | File | Size | Author |
---|---|---|---|
#16 | checked_working_2891276.png | 28.15 KB | jessicacs |
#16 | checked_dont_work_2891276.png | 27.89 KB | jessicacs |
#15 | 2891276_interdiff_14-15.txt | 867 bytes | hmendes |
#15 | 2891276-15.patch | 1.28 KB | hmendes |
#14 | 2891276-14.patch | 1.27 KB | hmendes |
Comments
Comment #2
mparker17Patch attached. Feedback welcome!
Comment #3
mparker17The 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 on8.x-1.0-alpha2
.Reviewers: please review the patch from #2 and test against
8.x-1.x-dev
.Comment #4
colan#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?
Comment #5
colanActually, let's postpone this until that gets done.
Comment #6
alabandit CreditAttribution: alabandit commented#3 does not seem to apply to 8.x-1.0-alpha4
Comment #7
yang_yi_cn CreditAttribution: yang_yi_cn commentedThe 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).
Comment #8
rfulcher CreditAttribution: rfulcher commentedI 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
Comment #9
colanFair 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!
Comment #10
paulmckibbenI've rerolled #2 due to the refactor of conditional_fields.api.inc into src/ConditionalFieldsFormHelper.php. It works for me locally.
Comment #11
agiraud CreditAttribution: agiraud commentedI've rerolled the patch for the latest version.
Comment #12
alfattal CreditAttribution: alfattal as a volunteer commentedI'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.
Comment #13
maskedjellybeanThe patch in #11 applies cleanly to the dev version and fixes my issue.
Comment #14
hmendes CreditAttribution: hmendes at CI&T commentedRe-rolling patch from #11.
Please review.
Comment #15
hmendes CreditAttribution: hmendes at CI&T commentedIt wasn't working when we had multiples conditions on the field.
Comment #16
jessicacs CreditAttribution: jessicacs at CI&T commentedI made the test with the patch #15 and it's worked.
Comment #18
colanThanks!
Comment #19
siliconmeadow CreditAttribution: siliconmeadow as a volunteer and commented...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.