【问题标题】:How to add a field to a specific page in multipage gravity form?如何以多页重力形式将字段添加到特定页面?
【发布时间】:2019-09-12 03:59:04
【问题描述】:

我正在使用 Gravity Forms 构建一个家庭订阅多页表单,并且我正在从第三方数据库中获取孩子。我想遍历孩子并显示他们每个人的姓名输入、等级下拉列表等。

我不知道如何将字段添加到表单中的特定页面。使用 gform_pre_render 钩子添加字段会将字段添加到表单的末尾。我已经尝试设置 pageNumber,但仍然没有成功。

function children_populate($form) {
// Add fields
    $new_field_id = 0;
    foreach( $form['fields'] as $field ) {
        if( $field->id > $new_field_id ) {
            $new_field_id = $field->id;
        }
    }
    $new_field_id++;
    $properties['type'] = 'text';
    $properties['pageNumber'] = 1;
    $field = GF_Fields::create( $properties );
    $field->id = $new_field_id;
    $field->label = 'Another New Field';
    $field->pageNumber = 1;
    $form['fields'][] = $field;

  return $form;
}
add_filter( 'gform_pre_render_2', 'children_populate' );
add_filter( 'gform_pre_validation_2', 'children_populate' );
add_filter( 'gform_pre_submission_filter_2', 'children_populate' );

以上,只是在表单末尾添加了字段。我想让它显示在我的 5 页表单的第二页上。

【问题讨论】:

  • 不确定GF是否真的按此页码对字段进行排序;当值从一个字段更改为下一个字段时,它可能只是以“控制中断”方式使用此值来确定新页面开始。因此,在这种情况下,您必须自己确保将新字段插入正确的位置。
  • 为此目的创建新数组可能是有意义的。循环遍历原始字段,并将字段一一插入到新字段中。当您到达要插入新字段的位置时(取决于您,您希望如何确定),插入该新字段,然后继续将其余原始字段添加到新数组中。最后用那个新数组覆盖 $form['fields']。

标签: php wordpress gravityforms


【解决方案1】:

您需要遍历表单对象以找到代表您要添加字段的页面的页面字段。然后您可以使用array_splice 将您的新字段添加到该页面的下一个索引之后 该页面字段。您仍然需要为每个子字段设置 $field->pageNumber 属性。

这个 sn-p 演示了这个概念,但不处理实际生成要插入到所需页面上的子字段数组。

$target_index = false;
foreach( $form['fields'] as $index => $field ) {
    // Update "2" to your target page number.
    if( $field->type == 'page' && $field->pageNumber == 2 ) {
        $target_index = $index + 1;
        break;
    }
}

array_splice( $form['fields'], $target_index, 0, $child_fields );

【讨论】:

    猜你喜欢
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    相关资源
    最近更新 更多