Work with Flexifields
nicholas.alipaz - January 30, 2009 - 21:06
| Project: | Conditional Fields |
| Version: | 6.x-1.0-beta2 |
| Component: | Javascript |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
Description
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.

#1
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?
#2
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".
#3
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"
#4
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!
#5
@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
#6
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 :-)
<?php
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
<?php
--- 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');
}
}
?>