Capture List Field Column as Comma-delimited List

Experimental Snippet 🧪

Code

Filename: gw-capture-list-field-column-as-comma-delimited-list.php

<?php
/**
 * Gravity Wiz // Gravity Forms // Capture List Field Column as Comma-delimited List
 * https://gravitywiz.com/
 *
 * Experimental Snippet 🧪
 */
// Update "123" to your form ID.
add_action( 'gform_pre_submission_123', function( $form ) {

	$list_field_id   = 4; // Update to your List field ID.
	$column_number   = 3; // Update to the column number you would like to fetch values from.
	$target_field_id = 5; // Update to the ID of the field to which you would like to capture values.

	$list_field = GFAPI::get_field( $form, $list_field_id );
	$rows       = $list_field->create_list_array( $_POST[ "input_{$list_field_id}" ] );
	$values     = array();

	if ( is_array( $rows[0] ) ) {
		foreach ( $rows as $row ) {
			$row      = array_values( $row );
			$values[] = $row[ $column_number - 1 ];
		}
	} else {
		foreach ( $rows as $row ) {
			$values[] = $row;
		}
	}

	$_POST[ "input_{$target_field_id}" ] = implode( ',', $values );

} );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.