I was able to set up and get Conditional Fields working very nicely when the CCK fields are not in a field group. However, when I create a field group and set one of the CCK fields within the group to control another within the same group, it fails to work. On further investigation, it appears that the ID reported by Drupal.settings.ConditionalFields.controlling_fields is #conditional-field- while the ID extracted from the DOM by jQuery is #conditional-group-. As a result, switchField does not catch this change in value.

I wonder if it would make sense for the markup to be altered so that all div.form-item elements in a fieldset are wrapped with a div with appropriate ID and class (just like the div.form-item of controlling fields of fields not in a fieldset)?

This is on Drupal 5.2 running CCK 1.2.2.3, fieldgroup 1.1.2.3 and Drupal 5.7.

Comments

konsumer’s picture

I had the same issue, and I think maybe the ideas I laid out here:

#227416: Custom conditional fields

would actually help with this too. It seems like both issues come from assumptions made about the parent IDs of the elements.

I worked out a hacked-together system that adds this on my own form_alter:

$conditional_fields=array(
    'controller_field_id'=>array(
      'child_id'=>array(1,2),
      'another_child_id'=>array(1,2),
      'yet_another_child_id'=>array(2),
    ),
    'controller_field_id'=>array(
      'another_child_id2'=>array('yes'),
      'another_child_id3'=>array('yes'),
    ),
  );

drupal_add_js(array('conditional_fields'=>$conditional_fields), 'setting');
drupal_add_js(drupal_get_path('module', 'mymod') .'/conditional_fields.js');

modules/mymod/conditional_fields.js looks like this:

$(function(){
  // for all field ids in Drupal.settings.conditional_fields set
  // the click handler to check the value, and hide/show fields based on it's value
  $.each(Drupal.settings.conditional_fields,function(key,val){    
    $('#'+key).click(function(){
      for (i in Drupal.settings.conditional_fields[this.id]){
        for (j in Drupal.settings.conditional_fields[this.id][i]){
          $('#'+i).parent().hide();
          if ($(this).val() == Drupal.settings.conditional_fields[this.id][i][j]){
            $('#'+i).parent().show();
            break;
          }
        }
      }
    });
    // initial value
    $('#'+key).click();
  });
});

In order to use it, put that form stuff into your own module's hook_form_alter. It's a quick hack, and only supports javascript (no form-level stuff, so required fields stay required, etc) but it works with any field id (use firebug to get the id of the actual controller/child element, like the select or checkbox.) All controlled elements need to have some kind of parent (but the default for all form fields is to be enclosed in div's, so this shouldn't be a problem, as long as you know.)

the format of the controller array is this:

array(
	'id_of_conditional_controller_element'=>array(
		'id_of_conditional_dependant_field'=>array('value to turn it on', 'another value to turn it on'),
	),
);

It's brain-dead simple, but it got it working for me the way I needed.

I think it'd be awesome to see this method incorporated, but unfortunately I don't have enough time to add it back into the conditional fields module. I may have a need for this functionality (with proper form-level stuff) soon, and in that case I will work on it.

I imagine that it would work well to add some auto-generation stuff to look for #conditional/#conditional_eval (as I mentioned in the other issue) to do some really cool stuff. combined with a function to grab the current conditional field info from the db, this would be a drop-in replacement that works with anything (not just cck fields, with bits of code wherever you want to control forms) and other modules could use it. It would also get rid of the must-be-same-fieldset issue.

I think this solution would actually fix a bunch of issues (on the javascript level, at least)

Here is a demonstration (with code to download):
http://jetboystudio.com/conditional-field-ideas

peterpoe’s picture

Status: Active » Fixed

The initial bug reported by djorn has long been fixed.
Please refer to #227416: Custom conditional fields for conditional fields hacking and customization.

Status: Fixed » Closed (fixed)

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