Skip Navigation

[Resolved] Setting the Default Value of a Dropdown when Importing Repeatable Group Data

This support ticket is created 2 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 1 reply, has 2 voices.

Last updated by Luo Yang 2 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#2207681

Im using this Post as an exmaple:

https://toolset.com/forums/topic/dynamically-populated-third-part-form-field-by-custom-field-of-the-child-post/

It works fine its just that IT loads the Post title of the child post (repeatable group) but I need to get and set the Default value of that Option as well:

Here is the code:

Ill explain data structure after:

add_filter('frm_setup_new_fields_vars', 'frm_populate_posts', 20, 2);
function frm_populate_posts($values, $field) {

// IDs of fields to be modified by this code for this relationship
$modify_fields = array( 29, 7 );
$relationship = "visa-cost-group";

if ( in_array( $field->id, $modify_fields ) ) {

// Get the parent post id from $post or wp_get_referer
// depending on whether initial page load or ajax request
if (wp_doing_ajax()) {
$url = wp_get_referer();
$parent_id = url_to_postid($url);
} else {
global $post;
$parent_id = $post->ID;
}

switch ( $field->id ) {
case '6':
$relationship = "provinces";
break;
case '29':
$relationship = "visa-cost-group";
break;
}

$posts = toolset_get_related_posts(
$parent_id, // get posts related to this one
$relationship, // relationship between the posts
'parent', // get posts where $parent_post is the parent in given relationship
999, 0, // pagination
array(), // arguments not required
'post_object',
'child'
);

unset($values['options']);
$values['options'] = array(''); //remove this line if you are using a checkbox or radio button field
$values['options'][''] = '';
foreach ($posts as $p) {
$values['options'][$p->ID] = array(
'label' => $p->post_title , //change 'Custom label' to your custom label
'value' => $p->types_render_field( "cost"), //change 'Default value' to your default value
);
}
$values['use_key'] = true; //this will set the field to save the post ID instead of post title
unset($values['options'][0]);
}
return $values;
}

What I tried Doing was settingt he options values to an array but that doesnt seem to work.

foreach ($posts as $p) {
$values['options'][$p->ID] = $p->post_title;
}

Works fine to Fill the dropdown with the titles of the Child post. But I need to set the default values on the form to as I need to perform calculations with the Field values ( i do this on the form builder formidable its self) i just need the Title/Values to fill

#2208663

Hello,

Since you are using filter hook of other plugins "Formidable Forms", you need to follow their document to setup the default option, see below document:
hidden link
section "Change the blank option label in Dynamic field"

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.