Hi @reventlov
I just got response from our developers and another version of code for you. This one should work fine with data taken right from the CSV file.
The code is:
<?php
add_filter(
'forminator_cform_render_fields',
function ( $wrappers, $model_id ) {
if ( $model_id != 6 ) {
return $wrappers;
}
$csv_map = array(
'select-1' => WPMU_PLUGIN_DIR . '/sheet1.csv',
);
foreach ( $wrappers as $wrapper_key => $wrapper ) {
if ( ! isset( $wrapper['fields'] ) ) {
continue;
}
if (
in_array( $wrapper['fields'][0]['element_id'], array_keys( $csv_map ) ) &&
isset( $wrappers[ $wrapper_key ]['fields'][0]['options'] )
) {
$csv_path = $csv_map[ $wrapper['fields'][0]['element_id'] ];
if ( ! file_exists( $csv_path ) ) {
continue;
}
$file = fopen( $csv_path, 'r' );
$new_options = array();
$opt_data = array();
while ( ( $line = fgetcsv( $file ) ) !== false ) {
$new_options[] = array(
'label' => sanitize_text_field( $line[0] ),
'value' => sanitize_text_field( $line[1] ),
'limit' => '',
'key' => forminator_unique_key(),
'calculation' => sanitize_text_field( $line[2] ),
);
$opt_data['options'] = $new_options;
}
fclose( $file );
$select_field = Forminator_API::get_form_field( $model_id, $wrapper['fields'][0]['element_id'], true );
if ( $select_field ) {
Forminator_API::update_form_field( $model_id, $wrapper['fields'][0]['element_id'], $opt_data );
$wrappers[ $wrapper_key ]['fields'][0]['options'] = $new_options;
}
}
}
return $wrappers;
},
10,
2
);
It should be added as Must Use plugin and you would also need to:
1. replace number 6 in this line
if ( $model_id != 6 ) {
with an ID of your form (form ID is the number you see in form’s shortcode)
2. in this part you define which select field should be filled-in and what’s the file name:
$csv_map = array(
'select-1' => WPMU_PLUGIN_DIR . '/sheet1.csv',
);
3. and the file (by default named “sheet1.csv”) should be uploaded
– either to the same /wp-content/mu-plugins folder
– or you can change the file path in the same like where you set file name by replacing WPMU_PLUGIN_DIR with the path
The CSV file structure to be used would be quite simple (no header line!)
New option 1, new-option-1, 123
New option 2, new-option-2, 99
and so on where the first column is option label, second is option value and third is calculation value (note: if you use decimals there, you cannot use comma separated format, so 10,99 may not work but 10.99 will).
Best regards,
Adam