Inserting a page dynamically with GF_Field::create breaks pagination [RESOLVED]

I’m trying to add a new pages dynamically using GF_Fields::create in a gform_pre_render filter, but pagination breaks. Example below.

    $pages[] = GF_Fields::create( array(
        'type'       => 'page',
        'id'         => (1000*$i),
        'formId'     => $form['id'],
        'size'       => 'large',
        'visibility' => 'visible',
        'displayOnly' => 1,
        'pageNumber' => $i,
        'nextButton' => array(
            'type' => 'text',
            'text' => 'Next',
            'id'   => (1000*$i),
        ),
        'previousButton' => array(
            'type' => 'text',
            'text' => 'Previous',
            'id'   => (1000*$i),
        ),
        'layoutGridColumnSpan' => 12,
    ) );

Then I add my new page into the form array, example below.

array_splice( $form['fields'], $position, 0, array( $pages[$i] ) );

This works in the sense that new pages are added to the web page markup if I check the HTML in the inspector. But the pagination won’t go further than the original number of pages. In this example there were originally 4 pages. Clicking next on the 4th page submits the form instead of proceeding to page 5.

I can see in the inspector that the ‘Next’ button triggers the javascript function below:

gform.submission.handleButtonClick(this)

So it’s like this javascript function isn’t aware of the new pages. Anybody know If I’m missing something?

You also need to update the $form['pagination']['pages'] property. That is an indexed array containing the page names.

The contents of the $form['pagination'] property is used when rendering the progress bar or steps, and by the form scripts that handle paging and page conditional logic.

You can find details about that property and the other properties the $form contains here: Form Object - Gravity Forms Documentation

1 Like

That seems to fix it. That and I also needed to use the filter gform_form_post_get_meta instead of gform_pre_render. Pagination now works as expected. Thanks!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.