By jvwissen on
I'm having an issue with the Form API.
When, in my situation, the form is submitted, it's submitted to "itself" ('/path').
The module_callback() function is called, and we make a rest_call... then the form is executed.
The submit handler gets called, and we do a redirect to '/path/#'.
Between the form is submitted and the submit handler, the module_callback() function is called,
and makes a rest_call... and then we're redirected..
What is the correct way to prevent most of the code in module_callback() to be executed?
See sample code below:
function module_menu() {
$items['path'] = array(
'title' => t('Some title'),
'page callback' => 'module_callback',
'access callback' => 'user_access',
'access arguments' => array('access content'),
);
}
function module_callback(...) {
$output = '';
if ($result = rest_call(....)) {
$output .= some_things_here($result);
$output .= drupal_get_form('module_some_form');
}
return $output;
}
function module_some_form(....) {
...
$form['#submit'][] = 'module_some_form_submit';
return $form;
}
function module_some_form_submit($form, &$form_state) {
if (is_numeric($form_state['values']['number'])) {
$form_state['redirect'] = '/path/'. $form_state['values']['number'];
}
}
Comments
A kludgy fix: You could just
A kludgy fix: You could just add a static variable to check if the callback function has been called before...
Now, since you tied module_callback() to the path, it will ALWAYS be called whenever a request is made to that path.
if all else fails, use a
if all else fails, use a static and check it
[edit]aaand I prolly should of read ur reply before posting mine[/edit]
Adding a static variable
Adding a static variable won't solve anything... at least not in the way I understand where and how to add a static variable in this situation.
The problem still exists, there's just no such thing in drupal to find out if a form was submitted.
Only way is to check the $_POST variable in my hook_menu callback function, imho that's dirty.
But I just can't find any solution for my problem.
It's a custom search module, that can be called from many different forms from different sources.
So It would be really great if drupal had some function(s) to find out what form source submitted itself.