From 2bf9ccd292ab042ae74a2b5b0c8c6d881a7f3039 Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Sat, 2 Mar 2024 07:22:04 -0600 Subject: [PATCH] Update value override handling for pre populating --- includes/Pods_GF.php | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/includes/Pods_GF.php b/includes/Pods_GF.php index 062a675..f1d0967 100644 --- a/includes/Pods_GF.php +++ b/includes/Pods_GF.php @@ -2250,6 +2250,8 @@ public static function gf_prepopulate( $form, $ajax = false, $prepopulate = null // Allow for value to be overridden by existing prepopulation or callback $value_override = $field_options['value']; + // @todo Fix prepopulating for field types: name and address (mapped to indiv inputs), file, consent, post_tag (multiple, comma separated), post_image. + if ( null === $value_override && isset( $gf_field->allowsPrepopulate ) && $gf_field->allowsPrepopulate ) { // @todo handling for field types that have different $_POST input names @@ -2462,13 +2464,40 @@ public static function gf_prepopulate( $form, $ajax = false, $prepopulate = null if ( is_array( $value_override ) && 'list' === $gf_field->type ) { $choices = $gf_field->choices; - $value_override_chunked = array_chunk( $value_override, count( $choices ) ); + $total_choices = count( $choices ); + + // Check if the values are chunked already. + if ( isset( $value_override[0] ) && is_array( $value_override[0] ) && $total_choices === count( $value_override[0] ) ) { + foreach ( $value_override as $vo_key => $value_override_row ) { + $value_override[ $vo_key ] = implode( '|', $value_override_row ); + } + + $value_override = implode( ',', $value_override ); + } else { + $value_override_chunked = array_chunk( $value_override, $total_choices ); + + foreach ( $value_override_chunked as $k => $v ) { + $value_override_chunked[ $k ] = implode( '|', $v ); + } - foreach ( $value_override_chunked as $k => $v ) { - $value_override_chunked[ $k ] = implode( '|', $v ); + $value_override = implode( ',', $value_override_chunked ); } - $value_override = implode( ',', $value_override_chunked ); + // @todo Figure this out, the 2.0 code had a count === 1 check that doesn't seem to make sense anymore. + + /*if ( is_array( $gf_field->choices ) && 1 === count( $gf_field->choices ) ) { + $choices = $gf_field->choices; + + $value_override_chunked = array_chunk( $value_override, count( $choices ) ); + + foreach ( $value_override_chunked as $k => $v ) { + $value_override_chunked[ $k ] = implode( '|', $v ); + } + } else { + $value_override_chunked = $value_override; + } + + $value_override = implode( ',', $value_override_chunked );*/ } $_GET[ 'pods_gf_field_' . $field ] = pods_slash( $value_override ); @@ -2488,6 +2517,8 @@ public static function gf_prepopulate( $form, $ajax = false, $prepopulate = null } } + pods_debug( $_GET ); + return $form; }