Hello :)
I am creating a web page. And after submitting the node I need to display a web page saying "Thank you for posting your content!" instead of the node itself.
The task looks simple, but it's not working as I expected. In Drupal 6 I could do
$form['#redirect'] = "somewhere";
but after some gooogling I found out that this was removed in Drupal 7, so now I need to do:
$form_state['redirect'] = "somewhere";
Unfortunately it did not work - after submitting the node form the node was displayed. I had the fallowing code in my module:
/**
* Implementation of hook_form_alter()
*/
function MY_MODULE_form_alter(&$form, &$form_state, $form_id)
{
switch($form_id) {
case 'bylos_node_form':
$form['#submit'][] = '_MY_MODULE_custom_submit';
break;
default:
break;
}
}
/**
* Custom submit handler
*/
function _MY_MODULE_custom_submit($form, &$form_state) {
$form_state['redirect'] = url('thank-you', array('absolute' => true));
}
As I found out that after my custom submit the node_form_submit() (line 404 in node.pages.inc) function is called and after the line
$form_state['redirect'] = 'node/' . $node->nid;
my custom redirect fails.
I could do simpe core hack by replacing the line above with:
if (!isset($form_state['redirect'])) {
$form_state['redirect'] = 'node/' . $node->nid;
}
But whenever I hack the core, a kitten dies. :)
So, how can I do simple form redirect without hacking the core? :)
Comments
Can't you just use a
Can't you just use a drupal_goto() even if it's an ugly solution, or is the saving process not finished yet?
Maybe you can also try to look at the new option to hook a form of a specific ID, no idea if it will help your problem but it's worth looking at. (Check it)
If it doesn't work you might consider adding this to the bugtracklist, it feels like a problem because you're hook gets overwriting by core...
I'm having the same issue.
I'm having the same issue. Can't figure out how to redirect to a different path on node form submission. Using drupal_goto() in hook_node_insert() is hacky, but works in the meanwhile.
Just figured out how to make
Just figured out how to make redirect override work. Simply add :
In your submit handler.
Thanks a lot ;)
Thanks a lot ;)
rebuilt = true: no redirect
This should not be true in the case of D7, according to the API:
thanks, this was my problem
thanks, this was my problem
similar problem
and I'm new to the module development end of things so i'm lost.
any idea why this code won't work for me?
Just a wrong function name
Hey ;)
The name of your function is wrong:
and later:
So, fix the name of the function to: custommade_custom_submit($form, &$form_state) and it should be all right :)
Doh!
Thanks! Yeah that was an oversight. I've got this working, but in case anyone out there is tuned in, there's another little problem-
What I really want is to redirect right back to the same node the form is on- I'm trying to get this redirect going so that when a user submits a comment form they are directed back to the same url rather than that url/comment/reply or whatever it is.
Anyways, I can't seem to get the nid right. Here's the code. Thoughts?
To get relevant nid
@natehill here's an example code...
http://drupal.org/node/290462#comment-1035899
It worked for me.
Redirect node edit form to custom page on submission
I can confirm that the following code snippet worked for me in Drupal 7.14
Place this snippet in your custom module:
For me too.
For me too.
"$form['#submit'][] =" didn't work
"$form['actions']['submit']['#submit'][] =" did work.
At least for a node form.
Mime slightly differs with Drupal 7.27
removed since it was not accurate.
Instead, please take a look at: https://drupal.org/node/1975230
Works perfectly
Thanks!
/chris
Forget my previous comment,
Forget my previous comment, using $form_state['rebuild'] is not the solution, node won't be saved this way...
After banging my head on the wall for hours, I realized that the submit handler should be set on the submit button and NOT on the form itself :
confirm
I can confirm this works. In this example you can also see how to redirect user to edit the same node again.
it is.
it is.
One addition for comment forms
Thanks for all comments, especially jide:
works well for normal forms!
One addition: for comment forms you can just use:
$form['actions']['submit']['#submit'][] works.
I, too, can confirm
$form['actions']['submit']['#submit'][] = 'MY_CALLBACK';does work while the documented
$form['#submit'][] = 'MY_CALLBACK';does not. I still don't get why. I added a watchdog call in my submit handler and it happily logged and overwrote $form_state['redirect'] but to no effect. I never got redirected while with the first solution I was. Is this intended behaviour? If not, what's the reason for this?
it works
Thank you, it works for me.
excellent
confirmed roadkill, it worked for me
thanks team
Use $form['#action']
There is another trick to do this -- to use $form['#action'] and destination GET var:
Best fix
I was trying to redirect the 'user/%/edit' form -- which by default doesn't go anywhere after submitting, just reloads -- and using the custom redirect handler worked, but none of the form information was saved to the database.
So this method worked when the other did not, and seemed to be very 'lightweight.'
Thanks for the tip!
form redirection in drupal 7
Form redirection in drupal 7 does not work like drupal 6 way. For security purpose $form['redirect'] is removed from drupal 7 to make it more secure.
in drupal 7 you can use it this way :--
in form_alter hook of your new module use this
$form['actions']['submit']['#submit'][] = 'overrided_submit_handler';
now put this code
function overrided_submit_handler($form, &$form_state)
{
$form_state['redirect'] = 'redirect path';
}
Vikas kumar
$form['#action'] seems to be a better solution
The $form['actions']['submit']['#submit'][] approach generally works. However, I've come across the strange behavior where the custom redirect is lost if the user clicks the "Add another item" for a multiple-value field on the node form and then submits it. The $form['#action'] solution suggested by ygerasimov doesn't fail in this case so that's what I ended up using.
Specifically 'node' form
It seems that $form['actions']['submit']['#submit'][] will work on normal forms, but not on the 'node' forms. Which I believe is the original question.
For some stupid unknown reason, node forms always seem to magically override my submits and will set it to just the 'node_form_submit' function. (very annoying)
Adding any other handlers for the node form either wipes the custom handlers, or hook_form_alter will only ever add the handlers BEFORE node's submit handler, which is where the 'redirect' happens.
If anyone knows how to get around this 'node' form issue (the right way), then please share, as I am wasting waaaaaaay to much time on a stupid node form.
I had the same problem on
I had the same problem on node forms. The second approach mentioned above works with node forms. Use:
$form['#action'] = '?destination=your_path';
Thanks, but message lost
Thanks, but the drupal_set_message() in submit handler is lost. After redirection I would expect the "Successfully saved" kind of message.
Update: I guess the form submission is not happening at all, http://drupal.org/node/330087#comment-1092257
Documentation on Redirecting Forms in D7
You do need to make a custom submit handler and put your redirect in there, but you don't want to completely override node_form_submit.
See this documentation page I just wrote for more details.
$form_state['redirect'] clarification in D7
This is an old issue, but one that still stumps people from time to time.
Interestingly $form_state['redirect'] redirects correctly when defined as follows in hook_form_alter():
However the redirect fails when defined as:
The solution is indeed as follows:
Labas-
Labas-
Another reason neither neither form_state['redirect'] nor drupal_goto worked in my case (just discovered) is because my form has to do with logging in a user, and part of the process invokes the hook_user_login (which happened to be implemented on this particular site by login_destination contrib module). What grief!
More: My custom form attempts to look up a user by other meta data or create a new user, and then logs in that user. After issuing user_login_finalize() (which in turn invokes hook_user_login) to make it all official, I couldn't do *anything* with redirecting like this. By the way, my drupal_set_message()s did still work.
My solution in this case was to either invoke hook_user_login like:
or add an exception to the login_destination configuration to not act when on this custom module's route/path. I implemented the latter and excluded my subscription lookup form page's path.
--
http://drupaltees.com
80s themed Drupal T-Shirts
Profiles 2 needs to save in #submit
This work for me!
$_GET['destination'] overrides the $form_state['redirect']
Take a look on the https://api.drupal.org/api/drupal/includes!form.inc/function/drupal_redi...
In my case there was a $_GET['destination'], which overrode my $form_state['redirect'].
$_GET['destination'] was the solution for me
nortmas is right. I struggle a lot with my form alter because sometimes there was a destination parameter in the URL that was overriding my $form_state['redirect'].
The solution for me was to write directly on the $_GET variable in the HOOK_form_alter:
This will always avoid the overriding. :-)
Thanks a lot nortmas.
ECK always overrides $form_state['redirect']
From eck.entity.inc (version 7.x-2.0-rc6):
So, if you are relying on the default ECK form submit handler, you have three choices:
1) Roll your own submit handler
2) Implement hook_drupal_goto_alter()
3) Use $_GET['destination']
Note that on #2 you will only have access to the destination set by ECK to work with. In my case I had a component called schedule, so ECK overrode $form_state['redirect'] and set it to "component/schedule/xxx". This is most unfortunate. That meant I had to reload the entity just saved with entity_load_single() to get the parts and pieces I needed to rebuild my redirect URL.
I ended up doing #3.
i've re-run into this trick
i've re-run into this trick myself.. i believe the trick is in ensuring that through all your _form functions to be sure to keep ($form, &$form_state) in all 3 grouped form functions. And follow expected form function name pattern (Drupal auto-magic)
the _alter method will work..but remembering to follow FAPI basics is the answer.
You can use the $_GET[
You can use the $_GET['destination']:
$_GET['destination'] vs. $form_state['redirect']
I've found $form_state['redirect'] doesn't work in certain instances, but $_GET['destination'] always works without fail.
This was the only solution
This was the only solution working for me on a webform submit :
$form_state['redirect'] = $path;inside a custom form submit somehow lost the original HTTP_REFERER as seen in form_alter.My use case was :
Return, after edit action from a webform submissions admin view row, back to the original view.
With an inherited codebase, I
With an inherited codebase, I was unable to get redirects working with
$form_state['redirect']on a very messy custom user_profile_form (WSOD crashes).I ended up just appending a ?destination= query string to
$form['#action']ie.
$form['#action'] .= '?destination=url/';$form_state['redirect'] work
$form_state['redirect'] work just fine.
If $_GET['destination'] is set, it must be a reason for that you have three option:
1) override the redirection:
2) Carry the destination to the page you redirect your form:
3) set $form_state['redirect'] and if $_GET['destination'] is set it will be used instead of your redirection
Thank you! It works like a
Thank you! It works like a charm!!!
For me #redirect works
For me #redirect works in drupal 7: