重力形式:动态创建字段集

Gravity Forms: Dynamically create set of fields

我想允许用户从下拉字段中select自定义post类型并向其中添加一些额外数据。

例如:用户可以 select 列表中的电影。对于那部电影,他可以添加一定数量的副本,直到他想借到为止。

所以我一共有三个字段:

  1. 带有电影的下拉字段
  2. 数字字段
  3. 日期字段

我现在想为 WordPress 中的每部电影添加此字段集(自定义 Post 类型)。 因为我不知道我们在WordPress中有多少电影,所以我想动态生成字段。

幸运的是,我从 Gravity Forms 找到了 Repeater (beta) 字段。 使用该字段,用户可以 add/remove 电影随心所欲。

演示和文档:https://docs.gravityforms.com/repeater-fields/

问题是,我需要用 WordPress 中的电影 CPT 填充第一个字段(下拉列表)。

这是我当前的代码,用于生成表单中的转发器字段:

// 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;
}

这是我在 WordPress 中用电影(自定义 Post 类型)填充字段的代码:

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;
}

但是我怎样才能结合这两个功能并用电影动态填充下拉字段?

我想我找到了解决方案:

在第一个 GF_Fields::create 之前,我必须从第二个函数中复制以下代码:

$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
            );
        }

然后我必须像这样编辑第一个 GF_Fields::create

$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
) );

新部分是 'choices' => $choices,,它从上面的代码中获取数据。当然,我必须将输入类型更改为 select:'type' => 'select',.