diff --git i/conditional_fields.module w/conditional_fields.module index 56b65b5..91d9830 100644 --- i/conditional_fields.module +++ w/conditional_fields.module @@ -210,6 +210,22 @@ function conditional_fields_element_info_alter(&$types) { } /** + * Implements hook_field_group_pre_render_alter() + */ +function conditional_fields_field_group_pre_render_alter(&$element, $group, &$form) { + // Add group name attribute for cf afer build + $element['#group_name'] = $group->group_name; + $parent = $group; + $groups = field_group_info_groups($form['#entity_type'], $form['#bundle']); + // Do we need parents? + // while (!empty($group->parent_name)) { + // $element['#parents'][] = $group->parent_name; + // $group = $groups[$group->parent_name]; + // } + // Attach states here eg. '#states' => array(...) +} + +/** * Processes form elements with dependencies. * * Just adds a #conditional_fields property to the form with the needed @@ -222,6 +238,10 @@ function conditional_fields_element_after_build($element, &$form_state) { if (isset($element['#field_name'])) { $field = $element; } + // elseif (isset($element['#group_name'])) { + // $field = $element; + // $field['#field_name'] = $field['#group_name']; + // } elseif (isset($element['#language'], $element[$element['#language']], $element[$element['#language']]['#field_name'])) { // Some fields are wrapped in containers before processing. $field = $element[$element['#language']]; @@ -229,7 +249,6 @@ function conditional_fields_element_after_build($element, &$form_state) { else { return $element; } - $form = &$form_state['complete form']; // Avoid processing fields in fields_ui administration pages. @@ -237,6 +256,9 @@ function conditional_fields_element_after_build($element, &$form_state) { return $element; } + // Get fieldgroup name if field belongs to one + $group = isset($form['#group_children']) && isset($form['#group_children'][$field['#field_name']]) ? $form['#group_children'][$field['#field_name']] : FALSE; + // Some fields do not have entity type and bundle properties. In this case we // try to use the properties from the form. This is not an optimal solution, // since in case of fields in entities within entities they might not correspond, @@ -252,16 +274,15 @@ function conditional_fields_element_after_build($element, &$form_state) { else { return $element; } - $dependencies = conditional_fields_load_dependencies($entity_type, $bundle); if (!$dependencies) { return $element; } - // Attach dependent. - if (isset($dependencies['dependents'][$field['#field_name']])) { - foreach ($dependencies['dependents'][$field['#field_name']] as $id => $dependency) { + $dependent = isset($dependencies['dependents'][$field['#field_name']]) ? $dependencies['dependents'][$field['#field_name']] : ($group && isset($dependencies['dependents'][$group]) ? $dependencies['dependents'][$group] : FALSE); + if ($dependent) { + foreach ($dependent as $id => $dependency) { if (!isset($form['#conditional_fields'][$field['#field_name']]['dependees'][$id])) { conditional_fields_attach_dependency($form, array('#field_name' => $dependency['dependee']), $field, $dependency['options'], $id); } @@ -355,7 +376,7 @@ function conditional_fields_element_after_build($element, &$form_state) { */ function conditional_fields_attach_dependency(&$form, $dependee, $dependent, $options, $id = 0) { $options += conditional_fields_dependency_default_options(); - + // The absence of the $id parameter identifies a custom dependency. if (!$id) { // String values are accepted to simplify usage of this function with custom @@ -398,6 +419,7 @@ function conditional_fields_attach_dependency(&$form, $dependee, $dependent, $op elseif (isset($dependent['#parents'])) { $dependent_parents = $dependent['#parents']; } + if (isset($dependent_parents)) { $form['#conditional_fields'][$dependent['#field_name']]['field_parents'] = $dependent_parents; $form['#conditional_fields'][$dependent['#field_name']]['dependees'][$id] = array( @@ -1266,21 +1288,34 @@ function conditional_fields_load_dependencies($entity_type = NULL, $bundle = NUL $default_options = conditional_fields_dependency_default_options(); $select = db_select('conditional_fields', 'cf') - ->fields('cf', array('id', 'options')) - ->orderBy('cf.dependent'); + ->fields('cf', array('id', 'options')); $fci_depende = $select->join('field_config_instance', 'fci_dependee', 'cf.dependee = fci_dependee.id'); - $fci_dependent = $select->join('field_config_instance', 'fci_dependent', 'cf.dependent = fci_dependent.id'); $select->addField($fci_depende, 'field_name', 'dependee'); - $select->addField($fci_dependent, 'field_name', 'dependent'); $select->addField($fci_depende, 'entity_type'); $select->addField($fci_depende, 'bundle'); + // Clone query now to allow a union with fieldgroup results + $select_group = clone $select; + + // Join with field_config_instance + $fci_dependent = $select->join('field_config_instance', 'fci_dependent', 'cf.dependent = fci_dependent.id'); + $select->addField($fci_dependent, 'field_name', 'dependent'); + + // Join with field_group + $group_dependent = $select_group->join('field_group', 'fg_dependent', 'cf.dependent = fg_dependent.id'); + $select_group->addField($group_dependent, 'group_name', 'dependent'); + if ($entity_type) { $select->condition( db_and() ->condition('fci_dependee.entity_type', $entity_type) ->condition('fci_dependent.entity_type', $entity_type) ); + $select_group->condition( + db_and() + ->condition('fci_dependee.entity_type', $entity_type) + ->condition('fg_dependent.entity_type', $entity_type) + ); } if ($bundle) { @@ -1289,7 +1324,15 @@ function conditional_fields_load_dependencies($entity_type = NULL, $bundle = NUL ->condition('fci_dependee.bundle', $bundle) ->condition('fci_dependent.bundle', $bundle) ); + $select_group->condition( + db_and() + ->condition('fci_dependee.bundle', $bundle) + ->condition('fg_dependent.bundle', $bundle) + ); } + // Union of results + //$select->orderBy('cf.dependent'); + $select->union($select_group, 'UNION'); $result = $select->execute(); diff --git i/includes/conditional_fields.admin.inc w/includes/conditional_fields.admin.inc index bf790f0..24be852 100644 --- i/includes/conditional_fields.admin.inc +++ w/includes/conditional_fields.admin.inc @@ -106,6 +106,15 @@ function conditional_fields_dependencies_overview_page($bundle_name = NULL, $ent function conditional_fields_dependency_add_form($form, &$form_state, $entity_type, $bundle_name) { $form = array(); $instances = field_info_instances($entity_type, $bundle_name); + // Get field groups on entity + $groups = field_group_info_groups($entity_type, $bundle_name); + $groups = $groups['form']; + foreach ($groups as &$group) { + $group = (array)$group; + } + // New merged array of fields & groups for dependents + // Dependees should only be fields ($instances) + $instances_groups = $instances + $groups; if (count($instances) < 2) { $form['no_fields'] = array( @@ -163,7 +172,7 @@ function conditional_fields_dependency_add_form($form, &$form_state, $entity_typ // first row they will appear grouped. if ($first_row == TRUE) { $form['table']['dependencies'][$id]['dependent'] = array( - '#markup' => check_plain($instances[$dependent]['label']) . ' (' . $dependent . ')', + '#markup' => check_plain($instances_groups[$dependent]['label']) . ' (' . $dependent . ')', '#rowspan' => $dependee_count, ); @@ -191,7 +200,7 @@ function conditional_fields_dependency_add_form($form, &$form_state, $entity_typ $row['description']['#colspan'] = 2; } - $row['description']['#markup'] = conditional_fields_dependency_description($instances[$dependency['dependee']]['label'], $instances[$dependent]['label'], $dependency['options']); + $row['description']['#markup'] = conditional_fields_dependency_description($instances[$dependency['dependee']]['label'], $instances_groups[$dependent]['label'], $dependency['options']); $row['edit'] = array( '#type' => 'link', @@ -221,6 +230,15 @@ function conditional_fields_dependency_add_form($form, &$form_state, $entity_typ asort($fields); + // Build list of available groups. + $fieldgroups = array(); + + foreach ($groups as $group) { + $fieldgroups[$group['id']] = check_plain($group['label'] . ' (' . $group['group_name'] . ')'); + } + + asort($fieldgroups); + // Build list of states. $states = array_map('drupal_strtolower', conditional_fields_states()); @@ -236,7 +254,7 @@ function conditional_fields_dependency_add_form($form, &$form_state, $entity_typ '#title' => t('Dependent'), '#title_display' => 'invisible', '#description' => t('Dependent'), - '#options' => $fields, + '#options' => $fields + $fieldgroups, '#prefix' => '
' . t('Add new dependency') . '
', ), 'dependee' => array( @@ -302,7 +320,6 @@ function conditional_fields_dependency_add_form_submit($form, &$form_state) { 'state' => $form_state['values']['state'], 'condition' => $form_state['values']['condition'] ); - if (!$id = conditional_fields_dependency_insert($form_state['values']['dependee'], $form_state['values']['dependent'], $options)) { drupal_set_message(t('There was an error while trying to create the dependency.'), 'error'); return; diff --git i/js/conditional_fields.js w/js/conditional_fields.js index a45961f..f8ea04b 100644 --- i/js/conditional_fields.js +++ w/js/conditional_fields.js @@ -139,4 +139,54 @@ Drupal.behaviors.conditionalFields = { } }; +Drupal.behaviors.cleanupFieldsets = { + attach: function (context, settings) { + $('fieldset', context).each(function(){ + var $fieldset = $(this); + + // Find out if the fieldset contains only elements that are hidden, + // regardless of the fieldset itself being hidden. + var countShownSubFields = 0; + $fieldset.find('.form-wrapper').each(function(){ + if ($(this).css('display') === 'block'){ + countShownSubFields++; + } + }); + + var data = $fieldset.data(); + // Horizontal tab support + if (data && data.horizontalTab) { + if (countShownSubFields === 0) { + $fieldset.data('horizontalTab').item.hide(); + } else { + $fieldset.data('horizontalTab').item.show(); + } + } + // Vertical tab support + else if (data && data.verticalTab) { + if (countShownSubFields === 0) { + $fieldset.data('verticalTab').item.hide(); + } else { + $fieldset.data('verticalTab').item.show(); + } + } + else { + if (countShownSubFields === 0) { + $fieldset.hide(); + } else { + $fieldset.show(); + } + } + }); + } +}; + +Drupal.behaviors.cleanupFieldsetsWatch = { + attach: function (context, settings) { + $('input, select', context).bind('click', function() { + Drupal.behaviors.cleanupFieldsets.attach($(this).closest('form'), Drupal.settings); + }); + } +}; + })(jQuery);