Checkboxes are not being handled correctly by conditional_fields/src/Plugin/conditional_fields/handler/OptionsButtons.php - tried in Druapl 8.3x and 8.4. Specifically:

protected function checkBoxesHandler($field, $field_info, $options) {

No longer works with the fields correctly as they are being rendered differently by Drupal 8.

An admin will get the following error messages when they try to work on forms:

"Notice: Undefined index: #return_value in Drupal\conditional_fields\Plugin\conditional_fields\handler\OptionsButtons->checkBoxesHandler() (line 107 of modules/contrib/conditional_fields/src/Plugin/conditional_fields/handler/OptionsButtons.php)."

This will not terminate form handling, however it will stop any conditional field settings from being applied, and will just show everything in the form as normal regardless of any conditional field settings applied.

Form fields do not have the right visibility metadata appended for example they have:
visible="[name=\u0022field_solution_type[]\u0022]"
instead of "[name=\u0022field_solution_type[]\u0022]":{"value":["24"]}}"

I am looking at a fix to this myself in the meantime for an urgent project and will get back with my findings, however you are probably likely to be able to produce a more stable patch.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

xiss23 created an issue. See original summary.

xiss23’s picture

Title: Checkboxes broken in Drupal 8.4 » Checkboxes broken in Drupal 8.x
Issue summary: View changes
Issue tags: -Drupal 8.4 +Drupal 8.x
colan’s picture

dpopdan’s picture

A quick fix

colan’s picture

Status: Active » Needs work
+++ b/src/Plugin/conditional_fields/handler/OptionsButtons.php
@@ -102,9 +102,9 @@ class OptionsButtons extends ConditionalFieldsHandlerBase {
+          $selector_key =conditional_fields_field_selector($field[$current_value]);

Missing space after "=".

emerham’s picture

Updated patch with code standards fixed.

Jonah Fenn’s picture

@dpopdan and @emerham, thanks for the patch. I've tested it out and it works like a charm.

colan’s picture

Status: Needs work » Needs review
SocialNicheGuru’s picture

Spokje’s picture

Re-roll for latest 8.x-1.x-dev and absence of patch in related issue (which fixes this problem at a more global scale).

Spokje’s picture

Tried to explain cause of (seemingly random) test failure on first test attempt in this issue: #3078778: Using RandomGeneratorTrait->randomStringas input for fields causes random test failures

cweldon’s picture

It looks like this also impacts "OR" and "XOR" when the field allows multiple values (e.g. it uses checkboxes). I can work on a patch to fix this but I am not sure if it should be posted here or as a new issue. While it is similar I do not want to hold up this patch from making it into production. Once I receive some feedback on what method is preferred (adding a patch here or creating a new issue) I will move forward with submitting the patch.

waako’s picture

Version: 8.x-1.x-dev » 4.x-dev

waako’s picture

Created MR with re-roll of patch in #10 for latest 4.x

Guido_S’s picture

I'm just facing a similar issue with checkboxes in drupal 9 (up to date) using the 4.0.0 alpha release.
I'm setting a price field as the target field and want the price field only to show up if the checkbox is set.
So I configure "the target field is visible when the control field has the value" and no matter if I choose "insert value from widget" and check the checkbox or leave it unchecked or choose "all values (AND)" and set it to 1 on the form it's always the same result.

Both fields are shown if the checkbox is unchecked and the price field gets hidden, when I check the checkbox instead of having it the other way round.

The data-drupal-states attribute on the price field is always:
data-drupal-states="{"visible":{"[name=\u0022field_field_name[value]\u0022]":{"checked":false}}}"

Can't test if the last patch already fixes this too, because it doesn't apply to the 4.0.0 alpha version or if it is even a completely different issue.

bbombachini’s picture

Tried the patch from the MR and it's still not working for me?

Core 9.3.6
Conditional fields 4.x-dev

Replicate:
- Create a list field and allow unlimited number of values.
- Display as checkboxes.
- Create 1 textfield for each option and create 2 conditions (make it visible and required for ex).
- On the dependency, set to use the widget value.
- On node edit form, try to select multiple options.

Expected result: dependent fields values are there because condition is met.
Result: If you click one checkbox it will show its dependent, if you check another one, not a single dependent field will show/work.

darshanchoudhary’s picture

Per my observation the issue is due to str_replace replacing the entire field name. Created a new patch which should only replace the key of the dependent field.

SocialNicheGuru’s picture

Status: Needs review » Needs work

I am seeing the same as #17.
Patch in #18 does not work

euk’s picture

Hi all!
I am experiencing the same issue as in #17.
The patch seems to work for me, however, I think for best results only the value in the last bracket of the selector should be replaced:

[name="field_field_name[another_group][VALUE]"]

Patch attached

euk’s picture

Re-rolling patch with fixed formatting...

euk’s picture

Title: Checkboxes broken in Drupal 8.x » Checkboxes broken

Updated issue title, as it is applicable to both 8.x and 9.x.

euk’s picture

Status: Needs work » Needs review
jurgenhaas’s picture

Status: Needs review » Reviewed & tested by the community

Tested #21 successfully, it fixes this problem.

  • heddn committed 3635c1e8 on 4.x authored by euk
    Issue #2926437 by euk, darshanchoudhary, SocialNicheGuru, jurgenhaas,...
heddn’s picture

Thanks for your contributions.

heddn’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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