Search code examples
phpwordpressgravity-forms-plugingravityforms

Gravity Forms: Dynamically create set of fields


I want to allow users to select a custom post type from a dropdown field and add some extra data to it.

For example: The user could select a movie from a list. For that movie he can add an amount of copies and till when he wants to borrow it.

So I have three fields in total:

  1. Dropdown field with movies
  2. Number field
  3. Date field

I now want to add this fieldset for every movie in WordPress (Custom Post Type). Because I don't know how many movies we have in WordPress, I want to generate the fields on the dynamically.

Fortunately I fount the Repeater (beta) field from Gravity Forms. With that field, the user could add/remove movies as he wants.

Demo and Docs: https://docs.gravityforms.com/repeater-fields/

The problem is, that I need to populate the first field (Dropdown) with the movies CPT in WordPress.

Here's my current code to generate the repeater field in the form:

// Adjust your form ID
add_filter( 'gform_form_post_get_meta_5', 'add_my_field' );
function add_my_field( $form ) {
    $movie_sku = GF_Fields::create( array(
        'type'   => 'text',
        'id'     => 1002, // The Field ID must be unique on the form
        'formId' => $form['id'],
        'required' => true,
        'label'  => 'Movie',
        'class'  => 'col-md-4',
        'pageNumber'  => 1, // Ensure this is correct
    ) );
    $movie_amount = GF_Fields::create( array(
        'type'   => 'text',
        'id'     => 1007, // The Field ID must be unique on the form
        'formId' => $form['id'],
        'required' => true,
        'label'  => 'Amount',
        'pageNumber'  => 1, // Ensure this is correct
    ) );
    $movie_date = GF_Fields::create( array(
        'type'   => 'text',
        'id'     => 1001, // The Field ID must be unique on the form
        'formId' => $form['id'],
        'required' => true,
        'label'  => 'Date',
        'pageNumber'  => 1, // Ensure this is correct
    ) );


    $movie = GF_Fields::create( array(
        'type'             => 'repeater',
        'required'          => true,
        'id'               => 1000, // The Field ID must be unique on the form
        'formId'           => $form['id'],
        'label'            => 'Add movie',
        'addButtonText'    => 'Add Another movie',
        'removeButtonText'=> 'Remove movie',
        'pageNumber'       => 1, // Ensure this is correct
        'fields'           => array( $movie_sku,$movie_amount, $movie_date), // Add the fields here.
    ) );

    $form['fields'][] = $movie;

    return $form;
}

// Remove the field before the form is saved. Adjust your form ID
add_filter( 'gform_form_update_meta_5', 'remove_my_field', 10, 3 );
function remove_my_field( $form_meta, $form_id, $meta_name ) {

    if ( $meta_name == 'display_meta' ) {
        // Remove the Repeater field: ID 1000
        $form_meta['fields'] = wp_list_filter( $form_meta['fields'], array( 'id' => 1000 ), 'NOT' );
    }

    return $form_meta;
}

And here's my code to populate a field with the movies (Custom Post Type) in WordPress:

add_filter( 'gform_pre_render_5', 'populate_posts' );
add_filter( 'gform_pre_validation_5', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_5', 'populate_posts' );
add_filter( 'gform_admin_pre_render_5', 'populate_posts' );
function populate_posts( $form ) {

    foreach ( $form['fields'] as &$field ) {

        if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-movie' ) === false ) {
            continue;
        }

        // you can add additional parameters here to alter the posts that are retrieved
        // more info: http://codex.wordpress.org/Template_Tags/get_posts

        $args = array(
        'orderby'       =>  'title',
        'order'         =>  'ASC',
        'numberposts'   => -1,
        'post_type'     => 'movie',
        'post_status'   => array('publish'),

        );

        $posts = get_posts( $args );

        $choices = array();

        foreach ( $posts as $post ) {

            $choices[] = array(
                'text'  => $post->post_title,
                'value' => $post->post_title
            );
        }

        // update 'Select a Post' to whatever you'd like the instructive option to be
        $field->placeholder = 'Select movie';
        $field->choices = $choices;

    }

    return $form;
}

But how can I combine these two functions and dynamically populate the Dropdown field with the movies?


Solution

  • I guess I found a solution:

    Before the first GF_Fields::create I have to copy the following code from my second function:

    $args = array(
            'orderby'       =>  'title',
            'order'         =>  'ASC',
            'numberposts'   => -1,
            'post_type'     => 'movie',
            'post_status'   => array('publish'),
    
            );
    
            $posts = get_posts( $args );
    
            $choices = array();
    
            foreach ( $posts as $post ) {
    
                $choices[] = array(
                    'text'  => $post->post_title,
                    'value' => $post->post_title
                );
            }
    

    Then I have to edit the first GF_Fields::create like this:

    $movie_sku = GF_Fields::create( array(
        'type'   => 'select',
        'id'     => 1002, // The Field ID must be unique on the form
        'formId' => $form['id'],
        'required' => true,
        'label'  => 'Movie',
        'choices'  => $choices,
        'pageNumber'  => 1, // Ensure this is correct
    ) );
    

    The new part is the 'choices' => $choices, which gets it's data from the code above. And of course I had to change the input type to select: 'type' => 'select',.