Bug #20696 New

ee()->functions->form_backtrack() does not work as expected on index page

Version: EE 2.9.2 Reporter: whitespace

The

form_backtrack()
function works by referencing a value within the
ee()->session->tracker

array.

0 => ‘pages/three’,
1 => ‘pages/two’,
2 => ‘pages/one’

If a form is declared with

ee()->functions->form_declaration()

then the form action URL is the site’s index page. Hence when the form is submitted, ‘index’ is pushed into the first position in the tracker array.

0 => ‘index’,
1 => ‘pages/three’,
2 => ‘pages/two’,
3 => ‘pages/one’

We can use

ee()->functions->form_backtrack('1')

to send the user back to the form page, which would be ‘pages/three’ here. (NB: the documentation for form_backtrack() at https://ellislab.com/expressionengine/user-guide/development/usage/functions.html#form-backtrack suggests that a negative number should be passed as the argument, but this didn’t work for me, and I can’t see how it would after looking at the code in /libraries/Functions.php)

However: the tracker functionality will not duplicate page references, so if the user is submitting a form from the site’s index page, the tracker array will not change – e.g. if the user has previously visited ‘pages/four’, the contents of the tracker array would be the same both before and after submission:

0 => ‘index’,
1 => ‘pages/four’

Thus, if we want to redirect the user back to the site index (where they submitted the form) having visited other pages prior to arriving on the index page,

ee()->functions->form_backtrack('1')

will send them to the immediately previous page (‘pages/four’ in the example above) instead of back to the site index.

Since this seems to be

form_backtrack()

‘s sole purpose, it would be helpful if it handled this special case automatically.

To get around this issue I’ve added a check as to whether the current page is the site index when declaring the form, and passing through an additional hidden form field if so:

$form_details = array(
 'id' => 'form_id',
 'hidden_fields' => array(
  'ACT' => $this->EE->functions->fetch_action_id('Module_name', 'function_name'),
 ),
);

if ($this->EE->session->tracker[0] == 'index') {
 $form_details['hidden_fields']['homepage'] = '1';
}

return $this->EE->functions->form_declaration($form_details);

Then, within the function bound to the action:

// redirect to index page if we came from there, previous page otherwise
header('Location: ' . ($this->EE->input->post('homepage', TRUE) ? $this->EE->functions->form_backtrack(0) : $this->EE->functions->form_backtrack(1)));
  • Nobody has said anything yet.

You must be signed in to comment on a bug report.

ExpressionEngine News

#eecms, #events, #releases