Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions gp-copy-cat/gpcc-copy-to-conditionally-hidden-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,23 @@
continue;
}

$source_field = GFAPI::get_field( $form, $target['source'] );
$source_values = $source_field->get_value_submission( array() );
$source_field = GFAPI::get_field( $form, $target['source'] );
$source_values = $source_field->get_value_submission( array() );
$condition_field = GFAPI::get_field( $form, $target['condition'] );
$condition_value = $condition_field->get_value_submission( $entry );

// for multi-input fields, we need to check the index
// to see if the condition is met.
if ( strpos( $target['condition'], '.' ) !== false ) {
list( $base, $index ) = explode( '.', $target['condition'] );
if ( isset( $condition_field['choices'][ (int) $index ] ) ) {
if ( $condition_field['choices'][ (int) $index ]['value'] !== $condition_value ) {
continue;
}
} else {
continue;
}
}
Comment on lines +39 to +48
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add support for non-choice conditions.

The current implementation only handles conditions for fields with choices. Consider expanding support to other field types that don't use the 'choices' property.

Add support for other field types by checking the field type:

 			if ( strpos( $target['condition'], '.' ) !== false ) {
 				list( $base, $index ) = explode( '.', $target['condition'] );
-				if ( isset( $condition_field['choices'][ (int) $index ] ) ) {
-					if ( $condition_field['choices'][ (int) $index ]['value'] !== $condition_value ) {
-						continue;
-					}
-				} else {
-					continue;
-				}
+				
+				// Handle different field types differently
+				if ( isset( $condition_field['type'] ) ) {
+					switch ( $condition_field['type'] ) {
+						case 'checkbox':
+						case 'radio':
+						case 'select':
+							if ( isset( $condition_field['choices'][ (int) $index ] ) ) {
+								if ( $condition_field['choices'][ (int) $index ]['value'] !== $condition_value ) {
+									continue;
+								}
+							} else {
+								continue;
+							}
+							break;
+						
+						default:
+							// For other field types, use a different comparison method
+							if ( $condition_value != $target['expected_value'] ) {
+								continue;
+							}
+							break;
+					}
+				} else {
+					continue;
+				}
 			}

Note: This implementation assumes there's an 'expected_value' property in the target configuration for non-choice field types. You'll need to adjust this based on your specific needs.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if ( strpos( $target['condition'], '.' ) !== false ) {
list( $base, $index ) = explode( '.', $target['condition'] );
if ( isset( $condition_field['choices'][ (int) $index ] ) ) {
if ( $condition_field['choices'][ (int) $index ]['value'] !== $condition_value ) {
continue;
}
} else {
continue;
}
}
if ( strpos( $target['condition'], '.' ) !== false ) {
list( $base, $index ) = explode( '.', $target['condition'] );
// Handle different field types differently
if ( isset( $condition_field['type'] ) ) {
switch ( $condition_field['type'] ) {
case 'checkbox':
case 'radio':
case 'select':
if ( isset( $condition_field['choices'][ (int) $index ] ) ) {
if ( $condition_field['choices'][ (int) $index ]['value'] !== $condition_value ) {
continue;
}
} else {
continue;
}
break;
default:
// For other field types, use a different comparison method
if ( $condition_value != $target['expected_value'] ) {
continue;
}
break;
}
} else {
continue;
}
}


if ( is_array( $source_values ) ) {
foreach ( $source_values as $input_id => $source_value ) {
Expand Down
Loading