I've created a multistep-form where the values of earlier steps are stored in $form_state['storage'].
Now, at the fourth step, I want to display some text filled in by the user in the first three steps. Because the display of the fourth step is a bit complicated, I thought it would be best to use a template file with a template_preprocess function. The goal of the template_preprocess function is to do some calculations based on what was filled in the first three steps and passing the results to the template.
The problem I encounter is I do not know how to get access to the $form_state['storage']. Only the form itself is available to the template_preprocess function. How can I get access to the $form_state array as well?

A part of my code:

<?php
/**
* _myModule_stepfour()
* This function got called by myModule_justaform() when it is in step 4.
* @param array $form
* @param array $form_state
* @return void
* @see
*	_myModule_stepfour_submit()
*/
function _myModule_stepfour(&$form, &$form_state)
{
	// (...)
	
	// Add a theme function
	$form['#theme'][] = 'myModule_justatest';	
}

/**
* _myModule_stepfour_submit()
* @param array $form
* @param array $form_state
* @return void
*/
function _myModule_stepfour_submit($form, &$form_state)
{
	$form_state['storage']['current_page'] = 4;
	$form_state['storage']['pages'][4] = $form_state['values'];
	// Note: in the real form, the page numbers are stored in constants, so adding an extra step would be easier
	
	// (...)
}

/**
* Implementation of hook_theme()
* @return array
*/
function myModule_theme()
{
	return array (
		'myModule_justatest' => array (
			'arguments' => array('form' => NULL),
      		'template'	=> 'justatest',
		),
	);
}

/**
* template_preprocess_uc_pod_bestelstappen_offerte()
* @param array $variables
* @param string $theme
* @return void
*/
function template_preprocess_myModule_justatest(&$variables, $theme)
{
	// Here I need the access to $form_state['storage'] to do some calculations based 
	// on what the user filled in, in the first three steps of the form
}
?>

I've tried to add the argument 'form_state' in the myModule_theme() function, but that didn't work.

Comments

rschwab’s picture

You could try adding $form_state to the $_SESSION variable and retrieving it in your template.php that way.

<?php
$_SESSION['mymodule_form_state'] = $form_state;

and then in template.php:

$form_state = $_SESSION['mymodule_form_state'];

?>

- Ryan

justageek’s picture

I think I'd use session tracking to store and retrieve your info, I don't think the preprocess functions are intended for your use case, really.

megachriz’s picture

Thanks for your replies, I just discovered that the contents of $form_state are stored deep in the form-array when the form-array is part of the variables. The form-array is so big that I missed it.

<?php 
$form_state = $variables['form']['#parameters'][1];
?>

@justageek
Basically the fourth step is used to display what is filled in in the first three steps, with a few calculations included. The fourth step is a sort of confirm page, after that the values get stored.

bkosborne’s picture

This does not seem to be the case for D7