From cef113fa44c17e9e371728af3a4d92e888e594c3 Mon Sep 17 00:00:00 2001 From: David Smith Date: Tue, 10 Oct 2023 17:22:51 -0400 Subject: [PATCH] `gppa-populate-checkboxes-as-choices.php`: Updated to look up source choice for each selected value so label and value are preserved in the populated choice. --- .../gppa-populate-checkboxes-as-choices.php | 49 ++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/gp-populate-anything/gppa-populate-checkboxes-as-choices.php b/gp-populate-anything/gppa-populate-checkboxes-as-choices.php index e92093e84..af5529284 100644 --- a/gp-populate-anything/gppa-populate-checkboxes-as-choices.php +++ b/gp-populate-anything/gppa-populate-checkboxes-as-choices.php @@ -1,23 +1,30 @@ form_id ); + $source_field_id = str_replace( 'gf_field_', '', rgar( $templates, 'value' ) ); + $source_field = GFAPI::get_field( $source_form, $source_field_id ); + + foreach ( $objects as $object ) { foreach ( $object as $meta_key => $meta_value ) { - if ( absint( $meta_key ) === absint( $field_id ) ) { + if ( absint( $meta_key ) === absint( $source_field_id ) ) { /** * Some fields such as the multi-select store the selected values in one meta value. * @@ -28,19 +35,15 @@ function gppa_populate_checkboxes_as_choices( $choices, $field, $objects ) { continue; } - if ( is_array( $meta_value ) ) { - foreach ( $meta_value as $value ) { - $choices[] = array( - 'value' => $value, - 'text' => $value, - ); - } - } else { - $choices[] = array( - 'value' => $meta_value, - 'text' => $meta_value, - ); + if ( ! is_array( $meta_value ) ) { + $meta_value = array( $meta_value ); } + + foreach ( $meta_value as $value ) { + $source_choice = $source_field->get_selected_choice( $value ); + $choices[] = $source_choice; + } + } } }