Thursday, July 7, 2011

Changing the redirect value of a Drupal node form in D6

How to redirect to a page after node form submission

Method 1:

Add a destination to the query string of a URL:

?q=node/add/page?destination=admin

Method 2:
<?php
function test_my_form($form_state) {
  $form['#action'] = url('comment/reply/'. $edit['nid']);
}

Method 3:
<?php
function test_my_form_submit($form, &$form_state) {
  drupal_goto("my_page", "sex=1&country=BE");
}

Method 4:

Create a custom module custom.module and place this code in it:
<?php
function custom_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'contact_mail_page':
      $form['#redirect'] = 'thank-you-page';
      break;
  }
}

Method 5:
Reference: http://www.brianvuyk.com/story-type/changing-redirect-value-drupal-node-form-d6

Changing the 'redirect' value of a Drupal node form in D6
Posted Tue, 07/28/2009 - 13:21 by Brian

Generally, when you want to change the location that a for redirects to after submission, you usually should set $form_state['#redirect'] within a call to hook_form_alter().

<?php
/**
* Implementation of hook_form_alter
*/
function custom_module_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'node_type_form') {
    // Set form redirect
    $form['#redirect'] = some/other/path;
  }
}


Unfortunately, that doesn't work for the node form. The node form has three submission buttons ('Submit', 'Preview', and 'Delete'), and each of these has it's own submission handler which sets the redirect value. Furthermore, the redirect values set in the submission handlers associated with the buttons overwrite $form['#redirect'] as set in the code example above.

In order to redirect the node form, you need to add your own submission handler to be executed after the default handler:

<?php
/**
* Implementation of hook_form_alter
*/
function custom_module_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'node_type_form') {
    // Overwrite the default node submission handler with our own.
    $form['buttons']['submit']['#submit'][] = 'my_module_example_form_submit'
  }
}


Since our submit handler is added to the submit array after the original node-form-submit handler and therefore executed later, we can overwrite $form_state['redirect'] variable:

<?php
/**
* Custom submit handler. Overwrites the form redirection variable.
*/
function my_module_example_form_submit($form, &$form_state) {
  $form_state['redirect'] = 'some/other/path';
}


That should do it - the node form should now redirect to whatever path your set in the submit handler.

No comments: