Custom redirect after form submit
Asked Answered
D

5

9

I have a form, which is a default block-administration form. It's the standard form people use to edit block contents, visibility etc. When the user saves the form, drupal redirects the user to the block admin page.

Now, i want to take the user to another page, eg. the home page, after submitting a block-administration form. There are several ways to achieve this, but drupal recommends using the hook_alter_form method as described here

I've written a .module file called 'formdest' containing the following:

function formdest_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'block-admin-configure':
    $form_state['redirect'] = '/home';
  break;
}
}

and the .info file to accompany it:

; $Id: custom.info,v 1.0 2011/01/01 21:55:00 author Exp $
name = formdest
description = form destination
package = Other
core = 6.x

version = "6.x"
project = "custom"
datestamp = "1229018427"

My custom module shows up in the module list and I can enable it, thus activiting the redirect. But when I test it, drupal still takes me to the block admin page instead of to the homepage..

There are no error messages in neither firebug or system log, so I'm a bit clueless. Any of you coding gods has any ideas?

Dutybound answered 6/5, 2011 at 8:21 Comment(0)
N
22

Add a submit handler into hook_form_alter(), and set the override there.

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'my_form') {
     $form['#submit'][] = 'my_submit_handler';
  }
}

function my_submit_handler(&$form, &$form_state) {
  $form_state['redirect'] = 'home';
}
Nitramine answered 6/5, 2011 at 8:37 Comment(4)
thanks for the suggestion Yorirou, I've just modified the module and added the submit handler. But the redirect still doesnt take me to home, and again no errors. I'm beginning to think i might be using the wrong form id or something like that, gonna do some more debuggingDutybound
edit - Right, Yorirou's snippet worked (my previous approaches also seemed to work), problem was i didnt use underscores in the form id. When i replaced the hyphens with underscores everything worked like a charm.Dutybound
small tip which can save some time: if form redirect does not work, ensure that $form_state["rebuild"] = FALSE;Toper
I've a question : how to redirect to previous page? $form_state['redirect'] = ???Harms
T
3

Its as simple as 1 2 3

  1. Create a Custom page with just title i.e. Thank you for your contacting us. Now go to "URL path settings" of this page and un-check "Automatic alias" and type your custom page url i.e. thank-you.

  2. Remove default URL aliases for web forms i.e. [node:title] and save settings. So that you page url would be site.com/thank-you

  3. Edit that Web Form -> Go to Form Setting tab, in "Redirection Location" section set "Custom URL" as of your newly created page in step 1.

Enjoy!

Transitive answered 13/9, 2012 at 11:21 Comment(0)
S
1

I've been struggling with doing a dynamic redirect in drupal 6 based on where the user came from and here is what I came up with, I hope it helps someone else:

function mymodule_form_alter(&$form, &$form_state, $form_id){
   if($need_to_redirect){
        /*add a form field to the form, you could also add this value to a 
              session or cookie,but if the user is logged in/out based on this
              action the session will be rebuilt*/
        $form['my_redirect']=array(
            '#type' => 'hidden',
            '#value'=>isset($form_state['post']['my_redirect'])?$form_state['post']['my_redirect']:trim(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_PATH),'/')
        );
   }
   if(isset($form_state['post']['my_redirect'])){
        /*if there is a redirect set for the form add an extra submit handler,
               this ensures that in cases like a node form our redirect will get
               set last*/
        $form['#submit'][]='my_custom_redirect';
        //unset this so it doesn't override our custom redirect
        if(isset($form['#redirect'])) unset($form['#redirect']);
    }
}
function my_custom_redirect($form,&$form_state){
    $router_item = menu_get_item($form_state['values']['my_redirect']);
    //make sure the user has access to this menu item, if not just ignore it
    if ($router_item && $router_item['access']) {
        $form_state['redirect']=$form_state['values']['my_redirect'];
    }
};

Obviously the $need_to_redirect should be replaced with a switch or if check depending on your form_alter preference

Spooner answered 3/8, 2012 at 20:39 Comment(1)
Yeah perfect solution as form alter should not effect the form submissions by same form at other placesKrefeld
L
0

In form alter function, not using $form['#submit'][] = ..., instead using the following:

$form['actions']['submit']['#submit'][] = 'MYMODULE_submit';

See https://www.drupal.org/node/1074616#comment-4218548

Lido answered 10/11, 2015 at 8:2 Comment(0)
M
-2

Something that drove me crazy and even pissed me off a little in everybody's answers is that using form_alter for redirection REPLACED the submit action and didn't simply APPEND my new function to it. In my case (and quite of few other threads I read), I still needed to perform the form action. I just wanted to redirect it somewhere else afterwards. My redirection was working fine, but my form was not being submitted or data being saved.

Once I went back and reread the form_alter documentation, I realized that $form['actions'] is an array and therefore can accept other actions into it's array! For anyone else struggling with this, don't use the

$form['actions']['submit']['#submit'][] = 'myform_redirect_to_myfunction';

Instead simply APPEND your callback to the $form array using array_shift or array_unshift (depending on what order you want the functions to execute:

array_shift($form['#submit'], 'myform_redirect_to_myfunction');

I hope this helps someone else.

Middle answered 10/9, 2013 at 1:1 Comment(1)
Just to explain why this has been downvoted: (a) The answers above do NOT replace existing submit actions (if that's what happened to you you did it wrong); (b) what you are suggesting - changing/adding the #submit function on a submit button - is different to changing/adding to the #submit functions on the main form; And (c) getting all emotional is unhelpful (especially when you're incorrect).Fulfillment

© 2022 - 2024 — McMap. All rights reserved.