Posted by maozet on October 7, 2009 at 11:26am
my module alter the user/registration using hook_form_alter, hook_menu
There is something wierd(to me anyway)
function mpr_form_alter(&$form, &$form_state, $form_id) {
drupal_set_message($form_id);
...more here....
...more here....
}the output is:
* user_register
* user_register
why does form_alter with 'user_register' (as form_id) gets executed twice??
Comments
Function defnition
Hello,
I am not expert at this, but $form_state should not have a leading & if you are using the hook for version 6 of Drupal.
Take a look at hook_form_alter.
Regards,
Sergio
Its not the '&'
Sorry, but this has nothing to do with the issue...
Maozet
www.drupaldir.com
That's strange. I have no
That's strange.
I have no good answer for you, except some basic forensics. Do you have hook_form_alter implemented in another module with another drupal_set_message statement? Have you tried printing out the $form variable to see if they are actually identical?
$form[] elementes
#1: No, I dont have another hook_form_alter with drupal_set_message(). When i comment the line both lines are gone.
#2: I did print_r($form). They are not identical because i manipulate $form
Maozet
www.drupaldir.com
strange, this does not happen
strange, this does not happen for me. I have a vanilla D6 install running on this machine and wrote this hook, just happen to put it in the block module:
<?phpfunction block_form_alter(&$form, $form_state, $form_id) {
drupal_set_message($form_id);
}
?>
and then go to my register page: localhost/drupal/user/register
and what pops into the status region is one printing of 'user_register'. Same with the user login form on the frontpage.
Are you using $form_state['rebuild']?
Doug Stumberger
www.raceonedesign.com
dstumberger --AT-- hotmail --DOT-- com
No, I am using $Form_state['storage']
Ok, I partly found the answer.
I use $form_state['storage']['some_data'] = True;
But why is that fires up my form_alter again?
Maozet
www.drupaldir.com
that's it. Here's form.inc,
that's it. Here's form.inc, line 134
<?php
// If $form_state['storage'] or $form_state['rebuild'] has been set
// and the form has been submitted, we know that we're in a complex
// multi-part process of some sort and the form's workflow is NOT
// complete. We need to construct a fresh copy of the form, passing
// in the latest $form_state in addition to any other variables passed
// into drupal_get_form().
if ((!empty($form_state['storage']) || !empty($form_state['rebuild'])) && !empty($form_state['submitted']) && !form_get_errors()) {
$form = drupal_rebuild_form($form_id, $form_state, $args);
...
?>
using storage triggers a rebuild of the form. I'm curious why you are using storage, what is the problem you're trying to solve?
Doug Stumberger
www.raceonedesign.com
dstumberger --AT-- hotmail --DOT-- com
But
Thanks for replying...
I use the line for debug
$form_state['storage']['debug'] = true;I saw this comment in form.inc but this happens before i even submit the form.
I am writing a multipage registration form, so i set up hook_menu
$items['user/register/foo'] = array(....
...
);
In my callback function i then
return drupal_get_form('user_register')Then in hook_form_alter i manipulate the form.
The strange thing is that it happens on the first time the form is shown
Maozet
www.drupaldir.com
ok so I haven't built a lot
ok so I haven't built a lot of multi-step forms, but I don't see the reason to use $form_state['storage'] before your form reaching its submit handler the first time. Use
<?php$form['vid'] = array('#type' => 'value', '#value' => $node->vid);
?>
for example to pass value from the form definition function through to validate or submit handlers. Modify form_state in the validate handler to pass values to the submit handler:
<?php
function test_form($form_state, $node) {
$form = array();
// Pass the node through for validate and submit processing.
$form['node']=array(
'#type' => 'value',
'#value' => $node,
);
$form["submit"]=array(
'#type'=>'submit',
'#value'=>t('Submit'),
'#weight'=>20,
);
return $form;
}
function test_form_validate($form, &$form_state) {
// Output the node's title.
drupal_set_message('Node title in validate: ' . $form_state['values']['node']->title);
// Modify the node.
$form_state['values']['node']->title = t('New Title');
}
function test_form_submit($form, &$form_state) {
// Output modified title set in validate.
drupal_set_message('Node title in submit: ' . $form_state['values']['node']->title);
}
?>
and then use $form_state[ 'storage'] to pass values from the submit handler back into the form definition when the form is rebuilt for multi-step. I don't see a reason to use ['storage'] except when rebuilding the form after submit.
Doug Stumberger
www.raceonedesign.com
dstumberger --AT-- hotmail --DOT-- com
Ok, ill try that
If we speak about form definition and submit flow...
Is there any way to force drupal not to submit to the form definition submit handler?
I wish to submit the form only after the last page.
i have tried to unset($form(['#submit']) and create another submit button with an alternate submit handler but its not working.
The form is submitted anyway to the form definition
Maozet
www.drupaldir.com
THere may be....but I don't
THere may be....but I don't think you want to turn off the submit handler. You want to use the submit handler for step in your multi-step form.
I think what you want to do is change the title of the submit button on your form to something like "next" until you reach the final step of your form, and then use "submit", but use your submit handler at each step to process the values you've received in that step an pass them forward using 'storage' to the next step. You can use the form definition to pass a 'step' value from step to step to help your submit handler keep track of which step you're on at each form submission.
Does that make sense?
http://drupal.org/node/101707 for some more details
Doug Stumberger
www.raceonedesign.com
dstumberger --AT-- hotmail --DOT-- com
Well...
Well, first thanks for replying.
There is still one more issue which isn't solved.
My senario is bit different then the one you suggested (http://drupal.org/node/101707). I guess it would be easier to impliment that once i manipulate my own data type/module.
My goal:
I have added many fields using the Profile(Core) to the registration form, I want to make that registration process as multipage registration form.
The problem:
The submit handler which declared in user.module executed when ever i post any data.
I was thinking about saving the original submit handler and set it to a different one which will manage all data & steps.
Once i reach the last step i would restore the original submit handler and do the post.
I tried to unset it and declare another submit handler (in my module). But thats not working, the original submit/validate handler always executed.
Any thought?
Maozet
www.drupaldir.com
Yes, I've done something like
Yes, I've done something like this. Basically you know the name of the function to call for your final submit, for example user_login_submit. So override the submit handler array altogether (for example setting
<?php$form["#submit"] = array ('your_handler_submit') ;
?>
in hook_form_alter) which will prevent the original submit handler from being called. Then manage the multistep submission of the form in your submit handler for each step in the registration process. On the final step, have your submit handler call the original submit handler directly. Validate handling can be done in a similar fashion.
Doug Stumberger
www.raceonedesign.com
dstumberger --AT-- hotmail --DOT-- com