Hey there!
Sooooooo… I am trying to get this bad boy to pre-select on a Multi-select.
To give you an idea of what this is doing. I’m using a Bulk Add I created to add in National Parks ($get_nps). Users have certain National Parks they affiliate with and that is already in their meta (u_nps). I then want to high-lite, on another form, which parks that user has already set for their “default”. I am using this code that I pilfered from:
https://legacy.forums.gravityhelp.com/topic/pre-select-an-option-based-on-user-data
add_filter( 'gform_pre_render', 'populate_45_8' );
function populate_45_8( $form, $field ) {
if( $form["id"] != 45 ) return $form;
if( $field["id"] != 8 ) return $form;
$field = GFFormsModel::get_field( $form, 8 );
$existing = $field['choices']; // grab the already-exisintg choices from the form
$user_id = get_current_user_id();
$user_data = get_userdata($user_id);
$capabilities = wp_capabilities;
$get_role = get_user_meta( $user_id, $capabilities, true );
$user_role = key($get_role);
$meta_key_used = 'u_nps';
$get_nps = get_user_meta( $user_id, $meta_key_used, true );
$existing = $field['choices']; // grab the already-exisintg choices from the form
$choices = array(); // set up the array
foreach($get_nps as $item) {
if($item != ' ') {
if($item['text'] == $get_nps) $selected = true;
if($item['text'] != $get_nps) $selected = false;
}
// rewrite it so the choice set in the profile is selected and all others are not
$choices[] = array('text' => $item['text'], 'value' => $item['value'], 'isSelected' => $selected);
$field['choices'] = $choices;
}
return $form;
}
And when I test it, it works fine:
GF_Field_MultiSelect Object
(
[type] => multiselect
[_is_entry_detail:GF_Field:private] =>
[_context_properties:GF_Field:private] => Array
(
)
[_merge_tag_modifiers:GF_Field:private] => Array
(
)
[id] => 8
[label] => National Parks
[adminLabel] =>
[isRequired] => 1
[size] => medium
[errorMessage] =>
[visibility] => visible
[storageType] => json
[inputs] =>
[choices] => Array
(
[0] => Array
(
[text] => Acadia National Park, Maine
[value] => Acadia National Park, Maine
[isSelected] => 1
)
[1] => Array
(
[text] => Arches National Park, Utah
[value] => Arches National Park, Utah
[isSelected] =>
)
But it does not work/high-lite in the actual form:
Any thoughts???