diff --cc conditional_fields.module index 42a506b,5531ba5..0000000 --- a/conditional_fields.module +++ b/conditional_fields.module @@@ -445,168 -465,162 +467,169 @@@ function conditional_fields_form_after_ $state_handlers = conditional_fields_states_handlers(); // Cycle all dependents. - foreach ($form['#conditional_fields'] as $dependent => $dependent_info) { - $states = array(); + foreach ($form['#conditional_fields'] as $parent_dependent_key => $parent_dependent_info) { + foreach ($parent_dependent_info as $dependent => $dependent_info) { + $states = array(); - if (empty($dependent_info['dependees'])) { - continue; - } + if (empty($dependent_info['dependees'])) { + continue; + } - $dependent_location = array_merge($dependent_info['parents'], array($dependent)); - $dependent_form_field = drupal_array_get_nested_value($form, $dependent_location); + $dependent_location = array_merge($dependent_info['parents'], array($dependent)); + $dependent_form_field = drupal_array_get_nested_value($form, $dependent_location); - foreach ($dependent_info['dependees'] as $dependency) { - $dependee = $dependency['dependee']; + foreach ($dependent_info['dependees'] as $dependency) { + $dependee = $dependency['dependee']; - if (empty($form['#conditional_fields'][$dependee])) { - continue; - } + if (empty($form['#conditional_fields'][$parent_dependent_key][$dependee])) { + continue; + } - $dependee_info = $form['#conditional_fields'][$dependee]; - $dependee_location = array_merge($dependee_info['parents'], array($dependee)); - $dependee_form_field = drupal_array_get_nested_value($form, $dependee_location); - // We need the data inside the language key. - $dependee_form_field = $dependee_form_field[$dependee_form_field['#language']]; + $dependee_info = $form['#conditional_fields'][$parent_dependent_key][$dependee]; + $dependee_location = array_merge($dependee_info['parents'], array($dependee)); + $dependee_form_field = drupal_array_get_nested_value($form, $dependee_location); + // We need the data inside the language key. + $dependee_form_field = $dependee_form_field[$dependee_form_field['#language']]; - $options = $dependency['options']; + $options = $dependency['options']; - // Load field edit behaviors. - // If this dependent has multiple dependees, only the logic of the first - // dependency will be taken into account. - if (!isset($behaviors)) { - $behaviors = conditional_fields_field_behaviors('edit', $options); - } + // Load field edit behaviors. + // If this dependent has multiple dependees, only the logic of the first + // dependency will be taken into account. + if (!isset($behaviors)) { + $behaviors = conditional_fields_field_behaviors('edit', $options); + } - // Determine if the dependee is in the form. - if (empty($dependee_form_field) || (isset($dependee_form_field['#access']) && $dependee_form_field['#access'] == FALSE)) { - // Apply orphan dependent behaviors. - /* - if (in_array(CONDITIONAL_FIELDS_FIELD_EDIT_HIDE_UNTRIGGERED_ORPHAN, $behaviors)) { - // TODO - $is_triggered = TRUE; + // Determine if the dependee is in the form. + if (empty($dependee_form_field) || (isset($dependee_form_field['#access']) && $dependee_form_field['#access'] == FALSE)) { + // Apply orphan dependent behaviors. + /* + if (in_array(CONDITIONAL_FIELDS_FIELD_EDIT_HIDE_UNTRIGGERED_ORPHAN, $behaviors)) { + // TODO + $is_triggered = TRUE; - if ($is_orphan && !$is_triggered) { - $form[$dependent]['#access'] = FALSE; + if ($is_orphan && !$is_triggered) { + $form[$dependent]['#access'] = FALSE; + } } + */ + if (in_array(CONDITIONAL_FIELDS_FIELD_EDIT_HIDE_ORPHAN, $behaviors)) { + $dependent_form_field['#access'] = FALSE; + } + unset($behaviors[CONDITIONAL_FIELDS_FIELD_EDIT_HIDE_UNTRIGGERED_ORPHAN]); + unset($behaviors[CONDITIONAL_FIELDS_FIELD_EDIT_HIDE_ORPHAN]); + continue; } - */ - if (in_array(CONDITIONAL_FIELDS_FIELD_EDIT_HIDE_ORPHAN, $behaviors)) { - $dependent_form_field['#access'] = FALSE; - } + unset($behaviors[CONDITIONAL_FIELDS_FIELD_EDIT_HIDE_UNTRIGGERED_ORPHAN]); unset($behaviors[CONDITIONAL_FIELDS_FIELD_EDIT_HIDE_ORPHAN]); - continue; - } - - unset($behaviors[CONDITIONAL_FIELDS_FIELD_EDIT_HIDE_UNTRIGGERED_ORPHAN]); - unset($behaviors[CONDITIONAL_FIELDS_FIELD_EDIT_HIDE_ORPHAN]); - - // Build a jQuery selector if it was not overridden by a custom value. - // Note that this may be overridden later by a state handler. - if (!$options['selector']) { - $options['selector'] = conditional_fields_field_selector($dependee_form_field); - } - else { - // Replace the language placeholder in the selector with current language. - $options['selector'] = str_replace('%lang', $dependee_form_field['#language'], $options['selector']); - } - - if ($options['condition'] != 'value') { - // Conditions different than "value" are always evaluated against TRUE. - $state = array($options['state'] => array($options['selector'] => array($options['condition'] => TRUE))); - } - else { - // Build the values that trigger the dependency. - $values = array(); - if ($options['values_set'] == CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET) { - $values[$options['condition']] = $options['value_form']; + // Build a jQuery selector if it was not overridden by a custom value. + // Note that this may be overridden later by a state handler. + if (!$options['selector']) { + $options['selector'] = conditional_fields_field_selector($dependee_form_field); + } + else { + // Replace the language placeholder in the selector with current language. + $options['selector'] = str_replace('%lang', $dependee_form_field['#language'], $options['selector']); } - elseif ($options['values_set'] == CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND) { - $values[$options['condition']] = count($options['values']) == 1 ? $options['values'][0] : $options['values']; + + if ($options['condition'] != 'value') { + // Conditions different than "value" are always evaluated against TRUE. + $state = array($options['state'] => array($options['selector'] => array($options['condition'] => TRUE))); } else { - if ($options['values_set'] == CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR) { - // XOR behaves like OR with added 'xor' element. - $values[] = 'xor'; + // Build the values that trigger the dependency. + $values = array(); + + if ($options['values_set'] == CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET) { + $values[$options['condition']] = $options['value_form']; } - elseif ($options['values_set'] == CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT) { - // NOT behaves like OR with switched state. - $options['state'] = strpos($options['state'], '!') === 0 ? drupal_substr($options['state'], 1) : '!' . $options['state']; + elseif ($options['values_set'] == CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND) { + $values[$options['condition']] = count($options['values']) == 1 ? $options['values'][0] : $options['values']; } + else { + if ($options['values_set'] == CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR) { + // XOR behaves like OR with added 'xor' element. + $values[] = 'xor'; + } + elseif ($options['values_set'] == CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT) { + // NOT behaves like OR with switched state. + $options['state'] = strpos($options['state'], '!') === 0 ? drupal_substr($options['state'], 1) : '!' . $options['state']; + } - // OR, NOT and XOR conditions are obtained with a nested array. - foreach ($options['values'] as $value) { - $values[] = array($options['condition'] => $value); + // OR, NOT and XOR conditions are obtained with a nested array. + foreach ($options['values'] as $value) { + $values[] = array($options['condition'] => $value); + } } - } - $state = array($options['state'] => array($options['selector'] => $values)); + $state = array($options['state'] => array($options['selector'] => $values)); - $dependee_form_state = isset($dependee_form_field['#field_parents'], $dependee_form_field['#field_name'], $dependee_form_field['#language']) ? field_form_get_state($dependee_form_field['#field_parents'], $dependee_form_field['#field_name'], $dependee_form_field['#language'], $form_state) : NULL; + $dependee_form_state = isset($dependee_form_field['#field_parents'], $dependee_form_field['#field_name'], $dependee_form_field['#language']) ? field_form_get_state($dependee_form_field['#field_parents'], $dependee_form_field['#field_name'], $dependee_form_field['#language'], $form_state) : NULL; - // Execute special handler for fields that need further processing. - // The handler has no return value. Modify the $state parameter by - // reference if needed. - foreach ($state_handlers as $handler => $handler_conditions) { - if (array_intersect_assoc($handler_conditions, $dependee_form_field) == $handler_conditions) { - $handler($dependee_form_field, $dependee_form_state, $options, $state); + // Execute special handler for fields that need further processing. + // The handler has no return value. Modify the $state parameter by + // reference if needed. + foreach ($state_handlers as $handler => $handler_conditions) { + if (array_intersect_assoc($handler_conditions, $dependee_form_field) == $handler_conditions) { + $handler($dependee_form_field, $dependee_form_state, $options, $state); + } } } - } - // Merge this dependency with the states array. - $states = array_merge_recursive($states, $state); + // Merge this dependency with the states array. + $states = array_merge_recursive($states, $state); - // Build effect settings for effects with options. - // TODO: add dependee key to allow different effects on the same selector. - if ($options['effect'] && $options['effect'] != 'show') { - $selector = conditional_fields_field_selector(drupal_array_get_nested_value($form, array($dependent_location[0]))); - $effects[$selector] = array( - 'effect' => $options['effect'], - 'options' => $options['effect_options'] - ); - } + // Build effect settings for effects with options. + // TODO: add dependee key to allow different effects on the same selector. + if ($options['effect'] && $options['effect'] != 'show') { - $selector = conditional_fields_field_selector(drupal_array_get_nested_value($form, array($dependent_info['parents'][0]))); ++ $selector = conditional_fields_field_selector(drupal_array_get_nested_value($form, array($dependent_location[0]))); + $effects[$selector] = array( + 'effect' => $options['effect'], + 'options' => $options['effect_options'] + ); + } - // Apply reset dependent to default if untriggered behavior. - if (in_array(CONDITIONAL_FIELDS_FIELD_EDIT_RESET_UNTRIGGERED, $behaviors)) { - // Add property to element so conditional_fields_dependent_validate() can - // pick it up. - $dependent_form_field['#conditional_fields_reset_if_untriggered'] = TRUE; - unset($behaviors[CONDITIONAL_FIELDS_FIELD_EDIT_RESET_UNTRIGGERED]); + // Apply reset dependent to default if untriggered behavior. + if (in_array(CONDITIONAL_FIELDS_FIELD_EDIT_RESET_UNTRIGGERED, $behaviors)) { + // Add property to element so conditional_fields_dependent_validate() can + // pick it up. + $dependent_form_field['#conditional_fields_reset_if_untriggered'] = TRUE; + unset($behaviors[CONDITIONAL_FIELDS_FIELD_EDIT_RESET_UNTRIGGERED]); + } } - } - // Execute custom behaviors. - if (!empty($behaviors)) { - foreach ($behaviors as $behavior) { - // Custom behaviors are callbacks. - $$behavior('edit', $form, $form_state, $dependent, $dependencies); + // Execute custom behaviors. + if (!empty($behaviors)) { + foreach ($behaviors as $behavior) { + // Custom behaviors are callbacks. + $$behavior('edit', $form, $form_state, $dependent, $dependencies); + } } - } - unset($behaviors); + unset($behaviors); - if (empty($states)) { - continue; - } + if (empty($states)) { + continue; + } - // Add validation callback to element. - _conditional_fields_element_add_property($dependent_form_field, '#element_validate', 'conditional_fields_dependent_validate', 'append'); + // Add validation callback to element. + _conditional_fields_element_add_property($dependent_form_field, '#element_validate', 'conditional_fields_dependent_validate', 'append'); - // Save the modified field back into the form. - drupal_array_set_nested_value($form, $dependent_location, $dependent_form_field); + // Save the modified field back into the form. + drupal_array_set_nested_value($form, $dependent_location, $dependent_form_field); - // Add the #states property to the dependent field. - drupal_array_set_nested_value($form, array_merge($dependent_location, array('#states')), $states); + // Add the #states property to the dependent field. + drupal_array_set_nested_value($form, array_merge($dependent_location, array('#states')), $states); + - $has_states = TRUE; ++ $has_states = TRUE; + } } - + if (empty($has_states)) { + return $form; + } + + $form['#attached']['js'][] = drupal_get_path('module', 'conditional_fields') . '/js/states_enhancements.js'; // Add effect settings to the form. if ($effects) { @@@ -701,7 -715,7 +724,7 @@@ function conditional_fields_dependent_v $form_state['conditional_fields_dependents_errors'] = $field_errors; } else { -- $form_state['conditional_fields_dependents_errors'] = array_merge($form_state['conditional_fields_dependents_errors'], $field_errors); ++ $form_state['conditional_fields_dependents_errors'] = array_merge($form_state['conditional_fields_dependents_errors'], $field_errors); } } @@@ -1877,4 -1888,4 +1902,4 @@@ function conditional_fields_features_ap 'file' => drupal_get_path('module', 'conditional_fields') . '/includes/conditional_fields.features.inc', ), ); --} ++}