When using the module flexifield, if you construct a content type with conditional fields then include the content type in another content type via a flexifield the conditional operation does not work.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

visualnotion’s picture

I'd like to see this functionality, as well. It'd really open the door to a lot of new options in content creation.

I've got a site I'm building for a football league and this would really simplify the way players, stats, etc. are created.

Any idea on what it would take to make this work?

peterpoe’s picture

Status: Active » Closed (won't fix)

Sorry, but Conditional Fields is completely dependent on CCK, and making it work with Flexifield would require a complete rewrite of every section of code the module. So I have to mark this as "won't fix".

abaddon’s picture

Version: 6.x-1.x-dev » 6.x-1.0-beta2
Status: Closed (won't fix) » Active

re-opening this because i think the original issue was misunderstood, it isnt about controlling flexifields, its just about conditional fields working when they are aggregated inside a flexifield which is then used as a field of its own, i suspect the issue is easier to fix than it seems,
ill appreciate any advice to this and ill look to see if i can have it fixed soon

EDIT: this boils down to adding the proper html/js to the targeted fields, even when inside flexifield, ive got this working in my case and im trying to find a solution that isnt a "hack"

Bilmar’s picture

Hi abaddon,

Could you please share your current method of getting it to work?
I am having trouble with my custom form template to work with conditional fields and maybe your method will help.

Thanks!

abaddon’s picture

@trupal218
you should open a new issue for other comments and link to it from here, ill answer there more if needed, and post your site's url, this is slightly offtopic here :-)

basically, the module adds a

wrapper around the controlling and controlled fields, like
<div id="conditional-field-pla-cor-cocktail-music" class="conditional-field controlling-field"> and
<div id="conditional-field-pla-cor-cocktail-music-t" class="conditional-field controlled-field">

around the input fields in question, it doesnt matter where exactly in the DOM this is long as it wraps only around the targeted input elements (the javascript searches for any input elements inside those divs), in hook_form_alter you can wrap this with #prefix, #sufix
and it also passes these into a javascript array, format is easy, something like the below

jQuery.extend(Drupal.settings, { "ConditionalFields": { "controlling_fields": { "#conditional-field-pla-cor-cocktail-music": { "#conditional-field-pla-cor-cocktail-music-t": { "field_id": "#conditional-field-pla-cor-cocktail-music-t", "trigger_values": { "yes": "yes" } } }, "ui_settings": { "animation": 1, "anim_speed": "fast" } }.....

as long as you have this in your theme, it will work, at least for the node editing form, which is the only part i care about for this project
for your custom theme, so check if the above works on your site, i dont see how the module could break for just a custom theme, or are you using flexifields too?

also, ill post some code tomorrow after i get a chance to clean it up and test more, as a patch to get flexifields working

abaddon’s picture

the below fixes my issue with conditional fields not working inside a flexifield,

it also fixed an issue ive had with unlimited values "Add more" fields with the ahah call breaking the flexifield, this should be treated separately but im a bit lazy about it - if anyone bumps into it, its related to the "#prefix"/"#suffix" inside the 'content_add_more_js' case of hook_form_alter, im just calling the regular processing flow on it and it seems to fix it..

anyway, please test, review and let me know if you have any questions, and how should i do this so that its included inside a release..

firstly, there is some custom code that could be made into a separate module, im using my own custom sitemodule to manage this, ive added some comments for this post to make the code a tad bit clearer :-)

function sitemodule_form_alter(&$form, $form_state, $form_id) {
        if ( isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id ) {
                $form['#after_build'][] = '_sitemodule_ab_cfnef' ; //process the form after flexifield had its turn
        }
} // function sitemodule_form_alter */


/**
 * Internal helper function.
 */
//* function _sitemodule_ab_cfnef
function _sitemodule_ab_cfnef($form, $form_state, $recursive = FALSE) {
        if ( ! isset($form_state['want form']) || $form_state['want form'] != TRUE ) { //ahah add more case
                if ( $form_state['submitted'] != TRUE ) { //first run were called with the original form, handle only second case with the new arguments
                        return $form ;
                }
                if ( ! $recursive ) { //ahah fix for other fields that will get broken if we dont add the necessary js
                        conditional_fields_node_editing_form($form, $form_state) ;
                }
        }
        foreach ( element_children($form) as $element ) {
                if ( $form[$element]['#theme'] == 'flexifield_multiple_values' ) {
                        foreach ( element_children($form[$element]) as $element2 ) {
                                if ( $form[$element][$element2]['#type'] != 'flexifield_default_widget' ) {
                                        continue ;
                                }
                                $subform = $form[$element][$element2]['value'] ;
                                $subtype = $subform['type'] ;
                                $subform['type'] = array('#value' => $subform['#flexifield_item_type']) ; //masquerade as a different form type to load the other node CF data
                                conditional_fields_node_editing_form($subform, $form_state, TRUE) ;
                                $subform['type'] = $subtype ;
                                $form[$element][$element2]['value'] = $subform ;
                        }
                } else if ( $form[$element]['#type'] == 'fieldset' ) { //handle groups that have fields in them
                        $subform = $form[$element] ;
                        $subform = _sitemodule_ab_cfnef($subform, $form_state, TRUE) ;
                        $form[$element] = $subform ;
                }
        }
        return $form ;
} // function _sitemodule_ab_cfnef */

second, i needed to patch the CF code to handle multiple groups of controlling/controlled fields that have the same field_name, so im using the field #id inside the js
the other thing this patch does is to allow multiple conditional_fields_add_js() calls but without adding repeated 'ui_settings' keys

--- backup/58/html/sites/all/modules/conditional_fields/conditional_fields.module       2009-06-09 23:35:41.000000000 +0000
+++ html/sites/all/modules/conditional_fields/conditional_fields.module 2009-11-06 01:52:05.000000000 +0000
@@ -508,14 +508,13 @@ function conditional_fields_forms_submit
 /**
  * Alteration of the node editing form
  */
-function conditional_fields_node_editing_form(&$form, $form_state) {
+function conditional_fields_node_editing_form(&$form, $form_state, $need_unique_ids = FALSE) {
   $type_name = $form['type']['#value'];

   // Do nothing if there are no conditional fields
   if (!$data = conditional_fields_load_data($type_name)) {
     return;
   }
-
   // Remove from data fields that user can't edit
   // and apply orphaned fields settings
   $orphaned_settings = variable_get('c_fields_edit_' . $type_name, C_FIELDS_ORPHANED_SHOW_TRIGGERED);
@@ -580,7 +579,11 @@ function conditional_fields_node_editing
   // To do: look if we should make this themeable
   foreach ($data as $row) {
     // Add javascript settings for this field
-    $settings['controlling_fields']['#conditional-' . conditional_fields_form_clean_id($row['control_field_name'])]['#conditional-' . conditional_fields_form_clean_id($row['field_name'])] = array('field_id' => '#conditional-' . conditional_fields_form_clean_id($row['field_name']), 'trigger_values' => $row['trigger_values']);
+    if ( $need_unique_ids ) {
+      $settings['controlling_fields']['#conditional-' . conditional_fields_form_clean_id($form[$row['control_field_name']]['#id'])]['#conditional-' . conditional_fields_form_clean_id($form[$row['field_name']]['#id'])] = array('field_id' => '#conditional-' . conditional_fields_form_clean_id($form[$row['field_name']]['#id']), 'trigger_values' => $row['trigger_values']);
+    } else {
+      $settings['controlling_fields']['#conditional-' . conditional_fields_form_clean_id($row['control_field_name'])]['#conditional-' . conditional_fields_form_clean_id($row['field_name'])] = array('field_id' => '#conditional-' . conditional_fields_form_clean_id($row['field_name']), 'trigger_values' => $row['trigger_values']);
+    }
     // To do: feature, add an array of controlled fields to js to allow for multiple controlling fields for a field.

     // Build helper arrays
@@ -601,15 +604,15 @@ function conditional_fields_node_editing
         $form[$element]['#conditional_fields_multiple'] = TRUE;
       }
       if ($controlling_fields[$element]) {
-        $form[$element]['#controlling_field'] = $element;
+        $form[$element]['#controlling_field'] = $need_unique_ids ? $form[$element]['#id'] : $element ;
         $form[$element]['#theme'] = array('conditional_fields_form_item');
       }
       else if ($controlled_fields[$element]) {
         if ($form[$element]['#required']) {
           conditional_fields_unset_required_field($form[$element]);
-          $required_fields[$element] = array('field' => $element);
+          $required_fields[$element] = array('field' => ($need_unique_ids ? $form[$element]['#id'] : $element));
         }
-        $form[$element]['#controlled_field'] = $element;
+        $form[$element]['#controlled_field'] = $need_unique_ids ? $form[$element]['#id'] : $element ;
         $form[$element]['#theme'] = array('conditional_fields_form_item');
       }
     }
@@ -617,7 +620,7 @@ function conditional_fields_node_editing
       // Groups
       if ($controlled_fields[$element]) {
         // Group markup is still hardcoded.
-        $form[$element]['#prefix'] = '<div id="conditional-' . conditional_fields_form_clean_id($element) . '" class="conditional-field controlled-field">';
+        $form[$element]['#prefix'] = '<div id="conditional-' . conditional_fields_form_clean_id($need_unique_ids ? $form[$element]['#id'] : $element) . '" class="conditional-field controlled-field">';
         $form[$element]['#suffix'] = '</div>';
       }
       // Fields in groups
@@ -625,12 +628,12 @@ function conditional_fields_node_editing
         // All required fields inside a conditional group must be handled by conditional fields
         if ($controlled_fields[$group_element] && $form[$element][$group_element]['#required']) {
           conditional_fields_unset_required_field($form[$element][$group_element]);
-          $required_fields[$group_element] = array('field' => $group_element, 'in_group' => $element);
+          $required_fields[$group_element] = array('field' => ($need_unique_ids ? $form[$element][$group_element]['#id'] : $group_element), 'in_group' => ($need_unique_ids ? $form[$element]['#id'] : $element));
           $form[$element][$group_element]['#theme'] = array('conditional_fields_form_item');
         }
         // Manage also conditional fields inside normal groups
         if ($controlling_fields[$group_element]) {
-          $form[$element][$group_element]['#controlling_field'] = $group_element;
+          $form[$element][$group_element]['#controlling_field'] = $need_unique_ids ? $form[$element][$group_element]['#id'] : $group_element ;
           $form[$element][$group_element]['#theme'] = array('conditional_fields_form_item');
         }
         else if ($controlled_fields[$group_element]) {
@@ -638,7 +641,7 @@ function conditional_fields_node_editing
           if ($form[$element][$group_element]['#theme'] == 'content_multiple_values') {
             $form[$element][$group_element]['#conditional_fields_multiple'] = TRUE;
           }
-          $form[$element][$group_element]['#controlled_field'] = $group_element;
+          $form[$element][$group_element]['#controlled_field'] = $need_unique_ids ? $form[$element][$group_element]['#id'] : $group_element ;
           $form[$element][$group_element]['#theme'] = array('conditional_fields_form_item');
         }
       }
@@ -920,6 +923,9 @@ function conditional_fields_add_js($sett
     drupal_add_js(array('ConditionalFields' => $settings), 'setting');
     drupal_add_js(drupal_get_path('module', 'conditional_fields') . '/conditional_fields.js');
     $js = TRUE;
+  } else {
+    unset($settings['ui_settings']);
+    drupal_add_js(array('ConditionalFields' => $settings), 'setting');
   }
 }
penguin25’s picture

Version: 6.x-1.0-beta2 » 6.x-1.0
FileSize
5.76 KB

The changes suggested in comment #6 worked perfectly for me - thanks abaddon for doing all this work.

However, the patch in the second half of #6 doesn't apply cleanly to the recently released 6.x-1.0 version of conditional_fields. The fixes were pretty simple, so I've attached an updated patch to this post. You still need the code from the first half of #6 as well as this patch!

peterpoe’s picture

Version: 6.x-1.0 » 6.x-2.x-dev
Component: Javascript » Compatibility w/ other modules
Status: Active » Needs work

Please use the 6.x-2.x-dev branch to test compatibility fixes.

#6 Abbadon your work is great. It has to be split it in smaller patches that address the different issues and reroll against 6.x.2.x-dev, then we can start committing.

abaddon’s picture

ill try to remember my self to work on this in a few weeks, (ive hit the same issue again but im on a tight deadline on this one so im just making it work)

j0e’s picture

FileSize
5.8 KB

Abaddon just sent me the latest patch for 6.x-1.1
So i'm posting it here for anyone who wants to integrate conditional fields and flexifield
Joseph

Rene Hostettler’s picture

These patches aren't working for me

I have created the helper module and applied the patch.

Have tested both the patched 6.1 and patched 6.1.1 version of the module with no success.

Does anyone have any suggestions of how to get them to work?

j0e’s picture

Could be wrong, but I remember trying out the beta version unpatched and having better success: http://drupal.org/node/717042

Can't remember why because it's not in the module description, so please don't blame me if it doesn't work. Also, if I remember correctly, you were mainly limited to using drop down menus to select fields.

Rene Hostettler’s picture

Hi thanks for your response jOe just tried that on your recommendation but no luck with beta unfortunately:( Guess I'm going to have to try and get my head around ahah with CCK

lhernon’s picture

subscribing

nicholas.alipaz’s picture

Status: Needs work » Active

I just tried both the beta and the current dev (as of today) and neither provide a solution for this issue. This is a real show stopper for us and seems like it could be fixed. Anyone working on this? I actually opened this issue in January of 2009... It is now July of 2010. That is a year and a half. Starting to wonder if there will ever be a supported fix ported to this module.

eRazor’s picture

Neither works for me. I don't find the part where the Drupal.settings javascript variable gets filled with the conditions declared in flexifield subtypes.

anavrin’s picture

Currently I use a flexifield module together with conditional fields modul. Generally everthing work fine ... however if I use a content types with conditional fields within other content types forms using flexifield field I lose functionality of conditional fields.

Have the same problem ... Anyone know the workaround for this? I tried all versions of conditional fields 1.0 + patch and extra module, 1.1, 2.0 beta2 , 2.0-dev ... none of them was working with flexifield :(

Can anyone tell how to make it work? (conditional field within flexifield?)

i'd appreciate if anyone could help me with this.

morybel’s picture

I haven't been able to work this out also... I would really really love this functionality. I used 6x.2.0 - beta 1 and 6.x-1.0 and 6.x-1.1. and nothing seams to work.
If anyone can help, it would be great. I appreciate any help.

Cheers

Frederic wbase’s picture

I have the same problem as mory none of the versions/patches work.
Is there any progress in this issue?

djdevin’s picture

FileSize
1.83 KB

We have a similar issue involving repeated calls to after_build functions, so we can have multiple node forms with conditional fields on one page.

Our solution was to queue the javascript going out, merge the calls to conditional_fields_add_js(), then add it after all other modules have finished after_build. This might not fix flexifield, but it might pave the way for how conditional fields logic gets added to the form.

Patch is against 2.x

peterpoe’s picture

The latest dev now supports multiple forms with cfields in the same page. I didn't use the same approach as #20, but rather opted to key the javascript settings by the form's #build_id, and changed conditional_fields.js accordingly, with the added benefit that the script is not confused by fields that might have the same id in different forms, since it limits the context of each state change to the form where the controlling field is located.

peterpoe’s picture

Issue summary: View changes
Status: Active » Closed (outdated)