I have a situation where i'd like to redirect a node_form to a different URL after it is submitted.
The correct approach to doing this is using the form API and using a hook form alter such as -
function mymodule_form_alter(&$form, $form_state, $form_id) {
if($form_id === 'node_form'){
$form['#redirect'] = array('url');
}
}
However if i do this the AJAX module does not respect my #redirect parameter in the $form array since it only looks in $form_state for the redirection.
my suggestion would be to first check the $form['#redirect'] if there is one, redirect there, else fall back on to $form_state['redirect']
does this sound sensible, or should we do this using another approach, for example have my module populate $form_state['redirect'] on a submit func before the ajax submitter is called??
if my suggestion sounds sensible, i'm attaching a patch to cover this.
| Comment | File | Size | Author |
|---|---|---|---|
| #21 | ajax_module_redirect.patch | 268 bytes | sprocketman |
| #19 | ajax_module_redirect.patch | 1015 bytes | usonian |
| #18 | ajax_module_redirect.patch | 757 bytes | usonian |
| #15 | ajax_module_redirect.patch | 626 bytes | sandfurz |
| ajax.module.patch | 346 bytes | tayzlor |
Comments
Comment #1
brendoncrawford commentedTaylzor,
Thanks for this feedback. You have a very good point here. I will add this fix shortly.
Comment #2
brendoncrawford commentedFixed in 6.x-1.x-dev. Please allow up to 12 hours for dev package to update.
Comment #3
brendoncrawford commentedFixed in 6.x-1.x-dev. Please allow up to 12 hours for dev package to update.
Comment #5
rconstantine commentedI looked and this patch is not in the latest dev version. What gives?
Comment #6
rconstantine commentedBy the way, #redirect can accept the following and all need to be checked for:
http://api.drupal.org/api/file/developer/topics/forms_api_reference.html...
Also, this should override destinations as well.
Comment #7
Jackinloadup commentedI am also having an issue with this. It has prevented me from using the ajax module. The redirections from either logintoboggan or logintoboggan are not working. Instead users are directed to the home page.
Comment #8
Jackinloadup commentedOops. I meant the redirections from either logintoboggan or *login_destination* are not working.
Comment #9
redijedi commentedsubscribing
Comment #10
phil88 commentedsubscribing
Comment #11
Delta Bridges commentedsubscribing
Comment #12
XerraX commentedsubscribing
Comment #13
akongz commentedsame here....
Comment #14
possum4all commentedsubscribing
Comment #15
sandfurz commentedassimilate this! :-)
Comment #16
Countzero commentedThanks a lot : patch from #15 works perfectly.
Comment #17
usonian commentedNot sure if this is related to (or complicated by) #360545: Error with Form Block module - I agree it would be useful to have programmatic control over any given form's #redirect property; the 'disable_redirect' plugin makes it all or nothing.
Comment #18
usonian commentedThe patch in #15 throws away the query string part of the redirect, if set - on further investigation I noticed that the ajax_get_redirect() function already supports the array format for redirects, so I rolled a new patch against 6.x-1.14 that does pretty much the same thing, but passes the complete $form['#redirect'] value to $data['redirect'].
Comment #19
usonian commentedOn further testing, I found the inverse problem with my own patch; in my case I wanted the default $form['#redirect'] to send the user someplace else... the patch worked in that case, but I also have a button that I want to submit via ajax and then *not* redirect. I discovered two issues:
After looking at Drupal.Ajax.response with Firebug I noticed that the test for skipping redirection is whether data.redirect === null, so on a hunch I set my $form_state['redirect'] to NULL instead of FALSE, and that worked perfectly.
Comment #20
gorgo commentedhmmm I'm afraid the patch still hasn't fixed this problem for me...
After applying it I now get the message that an email was sent but no redirection at all. it stays on front page.
I really appreciate your efforts, but is there any chance you could have another look? I can't seem to get it to work.
Thanks
Comment #21
sprocketman commentedThis is my first attempt at uploading a patch, so hopefully it works. The issue with the original code seems to be the second redirect if/then statement. It mixes the indices of $form and $form_state and likewise puts both $form and $form_state into the statement.
Comment #22
rickmanelius commentedNone of these patches worked for me... redirect is still being trumped... when i turn ajax off, i get the proper redirect and messaging...
----EDIT
I take that back... i was fighting for 2 hours and then i reapplied #15, and things seem to be working now.
Apologies :)
Comment #23
waltercat commentedDoes anyone plan on implementing this into the module so us rookies who don't know and are afraid to patch modules can update? Thanks!
Comment #24
andrew_k commented#15 worked for me
Comment #25
R-H commentedsubscribe, glad someone fixed this. After 4 hours of debugging at least I figured out what was causing the problem. now I can try to patch it.
Comment #26
brendoncrawford commentedif somebody submits a working patch which can pass review, I will apply it.
Comment #27
tamerzg commented#21 worked for me. It seems like bug in original code.