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
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Gravity Wiz // Gravity Forms // Consolidate Multiple List Fields into a Single List Field
* https://gravitywiz.com/
*
* Instruction Video: https://www.loom.com/share/8b45a92cf56249a982aa1aa6e1301778
*
* This snippet merges values from multiple list fields into a single list field.
*/
// Update "123" to your form ID.
add_action( 'gform_post_submission_123', function ( $entry, $form ) {
// Define source field IDs and target field ID.
$source_field_ids = array( 1, 5, 4 );
$target_field_id = 7;
Comment on lines +13 to +14
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 field validation

The snippet uses hardcoded field IDs without verifying that these fields exist or are list fields. This could lead to runtime errors if the form structure changes.

// Define source field IDs and target field ID.
$source_field_ids = array( 1, 5, 4 );
$target_field_id  = 7;

+// Validate that all required fields exist and are list fields
+foreach ( $source_field_ids as $field_id ) {
+	$field = GFAPI::get_field( $form, $field_id );
+	if ( ! $field || $field->type !== 'list' ) {
+		error_log( "GF List Consolidation: Source field {$field_id} is not a valid list field." );
+		return;
+	}
+}
+
+$target_field = GFAPI::get_field( $form, $target_field_id );
+if ( ! $target_field || $target_field->type !== 'list' ) {
+	error_log( "GF List Consolidation: Target field {$target_field_id} is not a valid list field." );
+	return;
+}
🤖 Prompt for AI Agents
In gravity-forms/gw-consolidate-multiple-list-fields-to-single-list-field.php
around lines 13 to 14, the code uses hardcoded field IDs without validating
their existence or type. Add validation to check that each source field ID
exists in the form and is a list field, and similarly verify the target field
ID. If any field is missing or not a list field, handle the error appropriately
to prevent runtime issues.


$combined = array();

// Loop through source field IDs and merge their unserialized values.
foreach ( $source_field_ids as $field_id ) {
if ( isset( $entry[ $field_id ] ) && ! empty( $entry[ $field_id ] ) ) {
$field_values = unserialize( $entry[ $field_id ] );
if ( ! $field_values || ! is_array( $field_values ) ) {
$field_values = array();
}
$combined = array_merge( $combined, $field_values );
}
}

// Re-index the combined array.
$combined = array_values( $combined );

// Serialize the combined array and update the target field and entry.
$finalSerialized = serialize( $combined );
$entry[ $target_field_id ] = $finalSerialized;

GFAPI::update_entry( $entry );
}, 10, 2 );
Comment on lines +36 to +37
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 error handling for entry update

The code doesn't handle potential errors when updating the entry. Adding error checking will help with debugging if issues arise.

-GFAPI::update_entry( $entry );
+$result = GFAPI::update_entry( $entry );
+if ( is_wp_error( $result ) ) {
+	error_log( "GF List Consolidation: Failed to update entry. Error: " . $result->get_error_message() );
+}
📝 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
GFAPI::update_entry( $entry );
}, 10, 2 );
$result = GFAPI::update_entry( $entry );
if ( is_wp_error( $result ) ) {
error_log( "GF List Consolidation: Failed to update entry. Error: " . $result->get_error_message() );
}
}, 10, 2 );
🤖 Prompt for AI Agents
In gravity-forms/gw-consolidate-multiple-list-fields-to-single-list-field.php
around lines 33 to 34, the call to GFAPI::update_entry($entry) lacks error
handling. Modify the code to capture the return value of GFAPI::update_entry and
check if it indicates an error. If an error occurs, log or handle it
appropriately to aid debugging and ensure issues during entry update are
detected.

Loading