Skip to content

Instantly share code, notes, and snippets.

@patrickfreitasdev
Last active September 5, 2023 15:38
Show Gist options
  • Save patrickfreitasdev/d7ad926561db4b598baeaf66478d790b to your computer and use it in GitHub Desktop.
Save patrickfreitasdev/d7ad926561db4b598baeaf66478d790b to your computer and use it in GitHub Desktop.
<?php
add_filter(
'forminator_cform_render_fields',
function( $wrappers, $model_id ) {
if( $model_id != 1441 ){
return $wrappers;
}
$select_fields_data = array(
'select-3' => 'post',
);
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' ] ] )
) {
$posts = get_posts( array( 'post_type' => $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) );
if ( ! empty( $posts ) ) {
$new_options = array();
$opt_data = array();
foreach( $posts as $post ) {
$new_options[] = array(
'label' => $post->post_title,
'value' => $post->post_title,
'limit' => '',
'key' => forminator_unique_key(),
);
$opt_data['options'] = $new_options;
}
$select_field = Forminator_API::get_form_field( $model_id, $wrapper['fields'][0]['element_id'], true );
if( $select_field ){
if( $select_field['options'][0]['label'] != $opt_data['options'][0]['label'] ){
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
);
add_filter(
'forminator_replace_form_data',
function( $content, $data, $fields ) {
if( $data['form_id'] != 361 ){
return $content;
}
if ( ! empty( $content ) ) {
return $content;
}
$form_fields = Forminator_API::get_form_fields( $data['form_id'] );
$data_field = '';
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
);
@whynotadv
Copy link

`<?php

add_filter(
'forminator_cform_render_fields',
function( $wrappers, $model_id ) {
if( $model_id != 2190 ){
return $wrappers;
}

	$select_fields_data = array(
		'select-4' => 'models',
	);

	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' ] ] )
		) {
			$posts = get_posts( array( 'post_type' => $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) );

			if ( ! empty( $posts ) ) {
				$new_options = array();
				$opt_data = array();
				foreach( $posts as $post ) {
					$new_options[] = array(
						'label' => $post->post_title,
						'value' => $post->post_title,
						'limit' => '',
						'key'   => forminator_unique_key(),
					);
					$opt_data['options'] = $new_options;
				}
				$select_field = Forminator_API::get_form_field( $model_id, $wrapper['fields'][0]['element_id'], true );
				if( $select_field ){
					if( $select_field['options'][0]['label'] != $opt_data['options'][0]['label'] ){
						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

);

add_filter(
'forminator_replace_form_data',
function( $content, $data, $fields ) {
if( $data['form_id'] != 2190 ){
return $content;
}

	if ( ! empty( $content ) ) {
		return $content;
	}

	$form_fields = Forminator_API::get_form_fields( $data['form_id'] );
	$data_field = '';
	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

);`

@whynotadv
Copy link

Not sure why when I paste the code it's breaking it up like that but hopefully you can see what I'm doing and help me figure this out.

@bgrrota
Copy link

bgrrota commented Sep 15, 2022

Hi @patrickfreitasdev,

I'm getting this error after submitting the form Fatal error: Uncaught TypeError: Illegal offset type in isset or empty in my case is this line.

if ( ! isset( $field_options[ $field_value ] ) && isset( $_POST[ $key ] ) ) { return sanitize_text_field( $_POST[ $key ] ); }

@patrickfreitasdev
Copy link
Author

Hi @bgrrota

I hope you are doing well, can you please create a ticket in WordPress.org > Forminator and we can verify it for you? I tested the code once again it still working, same for @whynotadv, kindly follow up from wordpress.org where we have more agents working.

@adambichler
Copy link

adambichler commented Jun 22, 2023

Hello @patrickfreitasdev, I'm looking to achive
-> dynamically fill a select field values
-> set one of the values as default, depending on the page/post viewed

I'm using the code above and it works almost as expected. The only problem I have is, that I want to set one of the options as default, depending on which post the form is displayed. I tried

$post_id = get_queried_object_id(); // = the currently viewed page/post id
foreach ($posts as $post) {
    $new_options[] = array(
    'label' => $post->post_title,
     'value' => $post->post_title,
    'limit' => '',
    'default' => ($post->ID == $post_id) ? true : false,
    'key'   => forminator_unique_key(),
);
$opt_data['options'] = $new_options;
}

but this doesnt work. Any idea on how to achive this?
Thanks, Adam

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment