Hello,

I installed the module and created a conditional dependency. In condition I selected "value", in values input mode I selected "Insert value from widget" and then I selected the value of the taxonomy that I want to execute the condition. In Interaction with other dependencies I selected "AND", in Form state I selected "Visible" and in effects "Show/Hide".

After performing this configuration, when I select the category that supposedly should show/hide the target field, it does nothing. The target field is always visible.

Any Suggestions?

Comments

originalcode created an issue. See original summary.

lee.cocklin’s picture

I made a patch for a project that fixes this issue (see below). You may have to change the module path for it to work in your project.

diff --git a/docroot/modules/contrib/conditional_fields/src/Plugin/conditional_fields/handler/OptionsButtons.php b/docroot/modules/contrib/conditional_fields/src/Plugin/conditional_fields/handler/OptionsButtons.php
index 70d0559..a208656 100644
--- a/docroot/modules/contrib/conditional_fields/src/Plugin/conditional_fields/handler/OptionsButtons.php
+++ b/docroot/modules/contrib/conditional_fields/src/Plugin/conditional_fields/handler/OptionsButtons.php
@@ -104,7 +104,11 @@ protected function checkBoxesHandler($field, $field_info, $options) {
       case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET:
         $selector = conditional_fields_field_selector($field);
         foreach ($options['value_form'] as $value) {
-          $selector_key = str_replace($field['#return_value'], current($value), $selector);
+          if(empty($field['#return_value']) && isset($field['#name'])) {
+            $selector_key = "[name=\"".$field['#name']."[".current($value)."]\"]";
+          } else {
+            $selector_key = str_replace($field['#return_value'], current($value), $selector);
+          }
           $checkboxes_selectors[$selector_key] = ['checked' => TRUE];
         }
         break;
nkoporec’s picture

Status: Active » Needs work

Hi @lee.cocklin see this post , on how to correctly create&upload a drupal patch.

BD3’s picture

Even though the patch is not in the proper format, I applied it manually and can confirm that this fixes the issue.

scott.whittaker’s picture

I can also confirm the patch works for me. I guess if someone can roll it into a valid patch we can quickly mark it RTBC and queue it for a future version :)

nkoporec’s picture

Status: Needs work » Needs review
StatusFileSize
new1.31 KB

Recreated @lee.cocklin patch to a proper format.Please reviewed it.

scott.whittaker’s picture

Just dropping a note here that this doesn't work when the form appears as in inline entity form.

I suspect that might be out of scope for this particular issue - I'm not sure if any conditional field stuff works as an inline entity form.

nkoporec’s picture

Yeah there is already an open issue.See it here Support for Inline Entity Form

icf-vpathak’s picture

Thanks for the patch @nkoporec! It worked in my case.

scott.whittaker’s picture

Works for me too

useernamee’s picture

Hello, I reproduced the bug, applied the patch and the bug is gone. Changing status to RTBC.

useernamee’s picture

Status: Needs review » Reviewed & tested by the community
thejimbirch’s picture

The patch in #6 applied to dev works for me also.

m_z’s picture

The patch #6 is great and makes the "Insert value from widget..." option working for checkboxes (by the way: the other options don't work either for checkboxes in the current version of conditional_fields module and Drupal 8.6).

Here are some modifications to the patch to match the Drupal coding standards:

-          $selector_key = str_replace($field['#return_value'], current($value), $selector);
+          if (empty($field['#return_value']) && isset($field['#name'])) {
+            $selector_key = '[name="' . $field['#name'] . '[' . current($value) . ']"]';
+          }
+          else {
+            $selector_key = str_replace($field['#return_value'], current($value), $selector);
+          }

I think that the solution of this patch should be transformed for the other "Set of values" options: "All these values (AND)..." etc.

m_z’s picture

I followed my suggestion to transform the solution of the #6 patch to the "Set of values" options (which I needed to show my target field if one of the term checkboxes (that match my specified term IDs in the conditional fields setting) of my "controlled by" field have been checked (and not all - what is the behavior of the "Insert value from widget..." if you set multiple options there).

I didn't create a patch because there are 2 open points (look for TODO in my comments in the code below), but I want to share my code which can be a basis for further work on this issue:

  /**
   * Return state for check boxes.
   */
  protected function checkBoxesHandler($field, $field_info, $options) {
    // Checkboxes are actually different form fields, so the #states property
    // has to include a state for each checkbox.
    $checkboxes_selectors = [];

    switch ($options['values_set']) {
      case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET:
        $selector = conditional_fields_field_selector($field);
        foreach ($options['value_form'] as $value) {
          /* OLD:
          $selector_key = str_replace($field['#return_value'], current($value), $selector);
          */
          // NEW: https://www.drupal.org/files/issues/2018-03-22/conditional_fields_fix_checkboxes_2912357_6.patch
          if (empty($field['#return_value']) && isset($field['#name'])) {
            $selector_key = '[name="' . $field['#name'] . '[' . current($value) . ']"]';
          }
          else {
            $selector_key = str_replace($field['#return_value'], current($value), $selector);
          }
          // end NEW
          $checkboxes_selectors[$selector_key] = ['checked' => TRUE];
        }
        break;

      case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX:
        // We interpret this as: checkboxes whose values match the regular
        // expression should be checked.
        foreach ($field['#options'] as $key => $label) {
          if (preg_match('/' . $options['value']['RegExp'] . '/', $key)) {
            $checkboxes_selectors[conditional_fields_field_selector($field[$key])] = ['checked' => TRUE];
            // NEW / TODO: Maybe the line above must me modified to make regex values mork for (multiple) checkboxes as 'controlled by' field.
          }
        }
        break;

      case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND:
        /* OLD:
        $values_array = explode("\r\n", $options['values']);
        */
        $values_array = (!is_array($options['values'])) ? explode("\r\n", $options['values']) : $options['values']; // NEW: Seems that $options['values'] can already be an array. - Question / TODO: Does the following check ("is_array($values_array)") make sense, because $values_array should always be an array, right?
        if (is_array($values_array)) {
          foreach ($values_array as $value) {
            /* OLD:
            $checkboxes_selectors[conditional_fields_field_selector($field[$value])] = ['checked' => TRUE];
            */
            // NEW
            if (empty($field['#return_value']) && isset($field['#name'])) {
              $selector_key = '[name="' . $field['#name'] . '[' . $value . ']"]';
              $checkboxes_selectors[$selector_key] = ['checked' => TRUE];
            }
            else {
              $checkboxes_selectors[conditional_fields_field_selector($field[$value])] = ['checked' => TRUE];
            }
            // end NEW
          }
        }
        else {
          /* OLD:
          $checkboxes_selectors[conditional_fields_field_selector($field[$options['values']])] = ['checked' => TRUE];
          */
          // NEW
          if (empty($field['#return_value']) && isset($field['#name'])) {
            $selector_key = '[name="' . $field['#name'] . '[' . $options['values'] . ']"]';
            $checkboxes_selectors[$selector_key] = ['checked' => TRUE];
          }
          else {
            $checkboxes_selectors[conditional_fields_field_selector($field[$options['values']])] = ['checked' => TRUE];
          }
          // end NEW
        }
        break;

      case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR:
        $checkboxes_selectors[] = 'xor';
      case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR:
      case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT:
        /* OLD:
        $values_array = explode("\r\n", $options['values']);
        */
        $values_array = (!is_array($options['values'])) ? explode("\r\n", $options['values']) : $options['values']; // NEW: Seems that $options['values'] can already be an array.
        foreach ($values_array as $value) {
          /* OLD:
          $checkboxes_selectors[] = [conditional_fields_field_selector($field[$value]) => ['checked' => TRUE]];
          */
          // NEW
          if (empty($field['#return_value']) && isset($field['#name'])) {
            $selector_key = '[name="' . $field['#name'] . '[' . $value . ']"]';
            $checkboxes_selectors[] = [$selector_key => ['checked' => TRUE]];
          }
          else {
            $checkboxes_selectors[] = [conditional_fields_field_selector($field[$value]) => ['checked' => TRUE]];
          }
          // end NEW
        }
        break;
    }

    $state = [$options['state'] => $checkboxes_selectors];

    return $state;
  }

Some hints:

  • this code includes the #6 patch with my code style improvements
  • I only tested the CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR case in detail and it seems to work with multiple options that should trigger the target field
  • the CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND is untested, but should work - but it can be achieved by checking multiple options in the CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET, too
  • I haven't used the CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX, CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR and CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT in the past, but the last 2 should work with my modifications above and for CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX there is an open todo...
  • my "OLD" and "NEW" comments should be deleted before a patch is created (and should only help to see / diff my modifications quickly)
ahmad abbad’s picture

Patch #6 works for me

colan’s picture

Status: Reviewed & tested by the community » Needs work

Updating status based on #14. #15 looks like a good place to start for whoever's interested in working on this.

m_z’s picture

@Colan: I can create a patch, but as stated in #15 there are 3 options that I didn't test because of missing experience how this options should work:

  • a) CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX
  • b) CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR
  • c) CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT

Perhaps you (or someone else) can help me with some test instructions how these options should behave.

Hypothetical setting:

"controlled by field": field_my_checkbox with 4 checkbox options:
- checkbox_option_1|Option 1
- checkbox_option_2|Option 2
- checkbox_option_3|Option 3
- checkbox_option_4|Option 4
"dependent field": field_my_dependent_textfield (that should be shown if the condition is met)

lets assume that the first 2 options should be selected in the conditional fields settings

for a) I have no idea how to test - so I need instructions
for b) field_my_dependent_textfield is visible if either "Option 1" or "Option 2" is checked, but NOT if both are checked, right?
for c) field_my_dependent_textfield is visible if neither "Option 1" nor "Option 2" nor both are checked, right?

colan’s picture

@M_Z: I wish I could offer some help, but I'm not really all that familiar with that stuff. I became a maintainer mostly so I could clean up the queue, and then cut some needed releases so can't really comment on this aspect of it. (Also, it's been a while since I did look at any of the module's code as I no longer need this for any current projects.) Sorry!

Sounds like you're now the expert. ;)

pyxio’s picture

this patch does not work for checkboxes on profiles.

m_z’s picture

@colan: it seems that the patch (and my improvements for the patch) is not longer needed for versions greater or equal than alpha-6

I tested it in a project and everything is working fine without the patch (and the code is totally different between alpha-4 and alpha-6 which could explain why it works now without the patch).

So the issue could be closed?

colan’s picture

Status: Needs work » Fixed

Great! Thanks for the analysis.

liquidcms’s picture

hmm.. so patch not requied in alpha6? that's unfortunate as it is still broken for me.

liquidcms’s picture

I have a hierarchical taxonomy and i had a couple modules being used to format it. I disabled the formatters to just display in default checkbox format and still conditional field has no impact.

liquidcms’s picture

nope, my bad.. it is being broken when using the Term Reference Tree module and it's formatter.

Status: Fixed » Closed (fixed)

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

liquidcms’s picture

this is still broken for me.. but i am able to do what i need just use value and specifically setting the term id (this is for a term reference field set as checkboxes).

m_z’s picture

@liquidcms : Can you please describe your configuration to make the error reproducable for others?

colan’s picture

Please create a new issue for that, if it's still a problem.