Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom redirect after form submit

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?

like image 381
William Lekatompessy Avatar asked May 06 '11 08:05

William Lekatompessy


People also ask

How do I redirect a form after a submission?

You'll also need to have created the redirect page or know the URL that you want to redirect users to. Open your form and then select Settings > Confirmations > Default Confirmations. Under Confirmation Type, you can then choose to redirect users to a page on your website or a specific URL.

How do I redirect a WordPress form after submitting?

To do this, go to Settings » Confirmation like before. This time, select Go to URL (Redirect) from the Confirmation Type dropdown menu. Then, paste the complete URL of the external page you'd like to send the user to in the field labeled Confirmation Redirect URL.


3 Answers

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';
}
like image 115
tamasd Avatar answered Sep 19 '22 17:09

tamasd


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!

like image 31
awan Avatar answered Sep 19 '22 17:09

awan


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

like image 24
Trey Avatar answered Sep 19 '22 17:09

Trey