douquan9826 2016-11-22 15:56
浏览 44

Gravity表示动态填充和高级自定义字段,选项未显示

I'm using an ACF field from the options page to populate a Gravity Forms drop down. The values are pulling from the field but displaying at the top of the form (in preview), not in the drop down. I'm sure this is an issue in how I'm building the form. Thanks for help in advance!

See Screenshot

//gravity forms dynamically populate from ACF

add_filter( 'gform_pre_render_1', 'populate_posts' );
add_filter( 'gform_pre_validation_1', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_1', 'populate_posts' );
add_filter( 'gform_admin_pre_render_1', 'populate_posts' );

function populate_posts( $form ) {

    global $choices;

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

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

        if( have_rows('core_values', 'option') ):

            while( have_rows('core_values', 'option') ): the_row();
                $choices[] = array( 'text' => the_sub_field('value_mstr_name'), 'value' => the_sub_field('value_mstr_name') );
            endwhile;


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

        endif;

    }

    return $form;
}
  • 写回答

2条回答 默认 最新

  • douan6815 2016-11-22 16:27
    关注

    I hadn't defined choices as an array AND for ACF, the function the_sub_field is like an echo of the sub_field but get_sub_field actually propagates it into the GF field object choices property.

    //gravity forms dynamically populate from ACF
    
    add_filter( 'gform_pre_render_1', 'populate_posts' );
    add_filter( 'gform_pre_validation_1', 'populate_posts' );
    add_filter( 'gform_pre_submission_filter_1', 'populate_posts' );
    add_filter( 'gform_admin_pre_render_1', 'populate_posts' );
    
    function populate_posts( $form ) {
    
        foreach ( $form['fields'] as &$field ) {
    
            if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
                continue;
            }
    
            if( have_rows('core_values', 'option') ):
    
            $choices = array();
    
                while( have_rows('core_values', 'option') ): the_row();
                    $choices[] = array( 'text' => get_sub_field('value_mstr_name'), 'value' => get_sub_field('value_mstr_name') );
                endwhile;
    
    
            // update 'Select a Post' to whatever you'd like the instructive option to be
            $field->placeholder = 'Select a Core Value';
            $field->choices = $choices;
    
            endif;
    
        }
    
        return $form;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧