Hello,
After submitting a form, I want my users to be redirected to another form if a checkbox is checked. The drupal path I want to redirect to is 'node/add/organization' . So far my checkbox displays fine. I am using hook_form_alter() to add the new checkbox field and include my custom function in the $form['#submit'] array of functions.
Here's my custom function's code:
function my_custom_function($form, $form_state) {
if (isset($form['redirect_checkbox']['#value']['redirect'])) {
$form_state['redirect'] = 'node/add/organization';
}
}
This however is not doing the trick.
Some more info I found hacking around my local development environment:
- I used the watchdog() function to see what functions are being called when I click on submit. Here's what I got (in order of execution) : node_form_validate, node_form_submit, menu_node_form_submit, my_custom_function
- I also add a watchdog to see what parameters are going to drupal_redirect_form() and saw that it was being called with $form_state['redirect'] = 'node/222'
Any help would be appreciated, thanks.
Comments
Try this
Hi,
Remember, if I've solved your issue. Please prefix your original post subject with [SOLVED]. If not, please post your form alter code so we can see what you are doing :)
The default value for a check box (checked state ) is 1. You can change that in your form definition (or alter) by using the
#default_valueelement. http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....I set up a quick test case and then ran it through my debugger. Developing via print statements lengthens the development cycle exponentially :)
Hope that helps.
Jason
Hi Jason, Thanks again for
Hi Jason,
Thanks again for looking at the code. I know I'm getting in the if block because I ran watchdog inside it and saw the output in the 'admin/reports/dblog' page. Here is what's in the hook_form_alter() if it sheds any light:
I've also tried variations placing my funciton at the beginning of the $form['#submit'] array to no avail. Now I'm trying to replace $form['#submit'] altogether and calling node_form_submit($form, $form_state) within my page, but that ends up in a 'server unavailable' message from the browser, not drupal... no php erros in my console...
- - turpana.com - -
Replace checkbox(es) with checkbox
Hi.
Your post says check box, not a check box group. Check box is documented here: http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.... .
Try this:
As I previously mentioned, by default Drupal sets the default value of a check box (in checked state) as 1. You can use the
#default_valueelement to change the value returned by the checked state.Also, please verify (by using your web browser) that the path you are trying to redirect actually renders a page (with form).
Hope that helps.
Jason
thanks, but I'm sure that the
thanks, but I'm sure that the code inside my custom function is running because I used the following and saw my message output to the log:
and I have also been to the path that I'm redirecting to with my browser. It is a form to add a node of type 'organization'. The path is 'node/add/organization'.
The issue I don't understand is why $form_state['redirect'] is not actually redirecting even though the line of code is running. I've tried running it without any if block also and it does not redirect. Is that the correct format for redirecting from a submit function? Can't find anything saying otherwise in the documentation.
- - turpana.com - -
Can you make sure that the
Can you make sure that the $form_state is a reference variable (ie) instead of
function my_custom_function($form, $form_state) {
// try this
function my_custom_function($form, &$form_state) {
if (isset($form['redirect_checkbox']['#value']['redirect'])) {
$form_state['redirect'] = 'node/add/organization';
watchdog('mymessage', 'executing within the if block of my custom function');
}
}
Hope this helps...
Yes, I had &$form_state in
Yes, I had &$form_state in the code, but copied by hand to the forum but mistyped it. That doesn't fix the issue I'm having.
- - turpana.com - -
Function does not return any value
I could be barking up the wrong tree, but to make the re-direction work, should you not have
drupal_redirect_form($form_state);as the last statement in your if{} block? (assuming this function is the last thing you do in the submit code)
I'm not sure what's
I'm not sure what's happening, but I tried adding
drupal_redirect_form($form, $form_state)(as per drupal_redirect_form drupal 6 api) at the end of my submit function (which is running last) but then my node is not saved. I added the 2 watchdog lines to form.inc to see what's being called. One just within the foreach $handlers ... and one just after it. Here's the code:and this is the output to the log:
Type Date Message User Operations
mymessage 28 Jan 2011 - 2:11pm *** node_form_submit post execution1 *** member
content 28 Jan 2011 - 2:11pm profile: updated member. member view
mymessage 28 Jan 2011 - 2:11pm *** my_custom_function post ... member
devel 28 Jan 2011 - 2:11pm adding redirect member
mymessage 28 Jan 2011 - 2:11pm *** my_custom_function pre execution *** member
mymessage 28 Jan 2011 - 2:11pm *** menu_node_form_submit pre execution *** member
mymessage 28 Jan 2011 - 2:11pm *** node_form_submit pre execution *** member
mymessage 28 Jan 2011 - 2:11pm *** node_form_validate post execution1 *** member
mymessage 28 Jan 2011 - 2:11pm *** node_form_validate pre execution *** member
It reads in reverse chronological order, since the log is ordered from newest to oldest. Here's what I don't understand:
The *** node_form_submit pre exececution *** message is logged before my_custom_function is called, but *** node_form_submit post execution is called after my_custom_function is called. Does anyone understand why this is the case? Even though my function is called last, node_form_submit finished executing last. Any ideas???
- - turpana.com - -
ahah! hahaha! here is the
ahah! hahaha!
here is the answer: http://drupal.org/node/144132#comment-3056754 (buried in a comment to a guide for migrating from 5.x to 6.x)
and the proper custom form_alter code:
The point is to add the custom function to the $form['buttons']['submit']['#submit'] array - not $form['#submit']
Thanks to everyone for your comments!
- - turpana.com - -