Hello there @alwanma27
This looks a bit complex indeed, so this is what our developers proposed.
We had a snippet to render users on select field. You can try modifying that and get the restaurant names from the DB and render on a Select A field but the second query looks too complex.
Something like this:
<?php
add_filter( 'forminator_cform_render_fields', function( $wrappers, $model_id ) {
if( $model_id != 361 ){
return $wrappers;
}
$select_fields_data = array(
'select-1' => 'restaurants',
);
foreach ( $wrappers as $wrapper_key => $wrapper ) {
if ( ! isset( $wrapper[ 'fields' ] ) ) {
continue;
}
if ( isset( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) && ! empty( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) ) {
$restaurant = 'Your_sql_query_to_get_restaurants';
if ( ! empty( $restaurant ) ) {
$new_options = array();
$restaurant_names = array();
foreach( $restaurant as $restaurant_names ) {
$new_options[] = array(
'label' => $restaurant_names,
'value' => $restaurant_names,
'limit' => '',
'key' => forminator_unique_key(),
);
$restaurant_names['options'] = $new_options;
}
$select_field = Forminator_API::get_form_field( $model_id, $wrapper['fields'][0]['element_id'], true );
if( $select_field ){
if( count($select_field['options']) != count($restaurant_names['options']) ){
Forminator_API::update_form_field( $model_id, $wrapper['fields'][0]['element_id'], $restaurant_names );
$wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ] = $new_options;
}
}
}
}
}
return $wrappers;
},10,2);
add_filter( 'forminator_replace_form_data', function( $content, $data, $original_content ) {
if( $data['form_id'] != 361 ){
return $content;
}
if ( ! empty( $content ) ) {
return $content;
}
$form_fields = Forminator_API::get_form_fields( $data['form_id'] );
foreach($data as $key => $value){
if ( strpos( $key, 'select' ) !== false ) {
$values = '';
$field_value = isset( $data[ $key ] ) ? $data[ $key ] : null;
if ( ! is_null( $field_value ) ) {
$fields_slugs = wp_list_pluck( $form_fields, 'slug' );
$field_key = array_search( $key, $fields_slugs, true );
$field_options = false !== $field_key && ! empty( $form_fields[ $field_key ]->raw['options'] )
? wp_list_pluck( $form_fields[ $field_key ]->options, 'label', 'value' )
: array();
if ( ! isset( $field_options[ $field_value ] ) && isset( $_POST[ $key ] ) ) {
return sanitize_text_field( $_POST[ $key ] );
}
}
}
}
return $content;
},10,3);
You will need to replace Your_sql_query_to_get_restaurants with the sql query and form ID from 361 to your form’s ID.
Thank you,
Dimitris