I have a simple menu callback that draws a page with a form on it. The page callback does a very expensive operation (requests a bunch of data from an external API).
function test_menu() {
$items = array();
$items['test'] = array(
'title' => 'Test',
'page callback' => 'test_page_handler',
'access callback' => TRUE,
);
return $items;
}
function test_page_handler() {
$data = very_expensive_data_operation();
$output = drupal_get_form('test_simple_form', $data);
return $output;
}
function test_simple_form(&$form_state, $data) {
$form['create'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function test_simple_form_submit(&$form, &$form_state) {
// do something
}
The problem I am seeing is that when the form is submitted, Drupal will re-execute the page callback in the background, resulting in the very_expensive_data_operation() running again.
There are probably lots of great reasons for Drupal to do this, but how can I change my app so that the very_expensive_data_operation() doesn't have to run again when submitting the form (after all, that's why I'm passing $data to the form, rather than having the form run it directly.)
One workaround I can think of is to toss $data into a cache table, but it feels like I'm missing something that available to me in the Drupal+FAPI api.