Skip Navigation

[Resolved] Filter options for parent select field in Forms

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a Form that allows Users to select a parent post, and I would like to filter the list of parent post options. Then I would like to associate the two posts using custom code.

Solution: You can do this with a generic field, the cred_save_data API, and the toolset_connect_posts API. You must be sure to set the generic field to use the option "persist":1, like this:

[cred_generic_field field='custom-parent-field' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"persist":1,
"options":[ ...your explicit options here... ]
}
[/cred_generic_field]

You can access a generic field value in the $_POST superglobal during the cred_save_data callback. You won't have access to the new post's ID in the cred_before_save_data callback. The new post hasn't been created yet, so no id yet. You'll need both post IDs to connect them in a relationship, so you will use cred_save_data instead. If your field has the name "custom-parent-field" then your callback would look like this:

add_action('cred_save_data', 'cred_custom_callback_fn',10,2);
function cred_custom_callback_fn($post_id, $form_data)
{
    $forms = array( 1234 );
    // if a specific form
    if (in_array($form_data['id'], $forms))
    {
        if (isset($_POST['custom-parent-field']))
        {
            $parent_id = $_POST['custom-parent-field'];
            $parent = toolset_connect_posts( 'relationship_slug', $parent_id, $post_id );
            // your other callback code continues here
        }
    }
}

Replace custom-parent-field with the name of your generic field. Replace 1234 with the ID of the Form, or a comma-separated list of Form IDs if you want to apply this same logic to more than one Form. Replace relationship_slug with the actual slug of this relationship.

Relevant Documentation:
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

This support ticket is created 6 years, 6 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 1 reply, has 2 voices.

Last updated by Christian Cox 6 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#915802

Hello.
I am presenting a form to add an item that has this relationship:

Animal > belongs to > Herd

The dropdown is presented correctly but I would like to further filter the results that populate the Herd dropdown when editing an Animal, based on a user_id of the logged in user.

The default CRED code for the dropdown is:

<div class="form-group">
<label>Herds Animals</label>
[cred_field field='@herd-animal.parent' select_text='--- not set ---' class='form-control' output='bootstrap']
</div>

I have tried to replace the code with a shortcode to replace the dropdown with an explicit set of filtered options which is showing in the form, but unable to save the results when I have attempted to save via 'cred_before_save_data' or 'cred_save_data'.

Is there an example where I can see how I could set the value of the parent relationship id, which appears as meta post data, using 'cred_before_save_data' ?

This is all on my local env currently, but I can send you some more details or get something posted if you need.

Thanks.

#915933

Hi, it depends on how you have set up the replacement select field, as well as the history of the post relationship. Here are some samples showing how to do this with a generic field, in a relationship created in Types 3.0+ (not migrated from the legacy Types system), using the cred_save_data and toolset_connect_posts APIs.

You must be sure to set the generic field to use the option "persist":1, like this:

[cred_generic_field field='custom-parent-field' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"persist":1,
"options":[ ...your explicit options here... ]
}
[/cred_generic_field]

You can access a generic field value in the $_POST superglobal during the cred_save_data callback. You won't have access to the new post's ID in the cred_before_save_data callback. The new post hasn't been created yet, so no id yet. You'll need both post IDs to connect them in a relationship, so you will use cred_save_data instead. If your field has the name "custom-parent-field" then your callback would look like this:

add_action('cred_save_data', 'cred_custom_callback_fn',10,2);
function cred_custom_callback_fn($post_id, $form_data)
{
    $forms = array( 1234 );
    // if a specific form
    if (in_array($form_data['id'], $forms))
    {
        if (isset($_POST['custom-parent-field']))
        {
            $parent_id = $_POST['custom-parent-field'];
            $parent = toolset_connect_posts( 'relationship_slug', $parent_id, $post_id );
            // your other callback code continues here
        }
    }
}

Replace custom-parent-field with the name of your generic field. Replace 1234 with the ID of the Form, or a comma-separated list of Form IDs if you want to apply this same logic to more than one Form. Replace relationship_slug with the actual slug of this relationship.

More information:
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts