13

I want to redirect users to a custom page, after they create a node or edit it.

6 Answers 6

6

You can use hook_form_alter to add a custom submit handler where you can set the redirection path.

See the FAPI for more details about form redirection.

7
  • 1
    Thanks for your answer ! i've read about the FAPI but a still have question to go ahead : Where should i put the hook_form_alter function ?
    – ousneo
    Commented May 9, 2011 at 10:24
  • 2
    @ousneo In drupal 7 you can put it in your theme or in a custom module you create.
    – googletorp
    Commented May 9, 2011 at 10:49
  • 1
    please see @Dmitry Vyal answer Commented Jan 11, 2012 at 14:57
  • 1
    Here's a module that does a redirect after saving. drupal.org/project/node_save_redirect
    – polyclick
    Commented Feb 18, 2014 at 10:32
  • 1
    While accepted, this answer is too brief to be useful. It really matters how you add your custom submit handler, and this answer is silent on that. Best answer IMHO is that from @leymannx (but see by comment about the edit use case). Commented Aug 16, 2018 at 10:30
4

You can create a rule that redirects the user to any page when they create or edit a specific content type. When you add a rule select "After Creating New Content" add also a condition of "Content Has Type". You Do section will give you the option to redirect a user to any page you want.

For a redirect after editing a node, just create a new rule and select "After updating Existing Content" as your first option.

5
  • I used Rules to redirect after creating a specific content, but the user gets no feedback now. I want to show the user that "Your content is created". How can I improve this ?
    – wallace740
    Commented Apr 24, 2012 at 14:59
  • 1
    Add another action after the redirect action to "Show a configurable message on the site" and add your custom message from there.
    – user2014
    Commented Apr 26, 2012 at 17:05
  • I added another Rule but i think I miss something in my custom theme because the message is not shown on my frontpage. I'm using a module 'Better messages' to show these kind of messages in a popup !rule config
    – wallace740
    Commented Apr 27, 2012 at 9:24
  • My experience is that overlay breaks Rules redirecting, beware. Commented Jul 3, 2012 at 23:29
  • In my case this the node add form was embedded insider a panel. hook_form_alter didn't catch it. But rules worked great!
    – Druvision
    Commented Feb 9, 2014 at 16:23
3

When doing this, ensure to also unset a possible existing destination which normally is added to the URL when you click the edit button to edit an existing node.

Replace article in MYMODULE_form_article_node_form_alter with your content type's machine name.

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function MYMODULE_form_article_node_form_alter(&$form, &$form_state, $form_id) {

  $form['actions']['submit']['#submit'][] = '_MYMODULE_node_form_redirect';
}

/**
 * Submit callback.
 */
function _MYMODULE_node_form_redirect($form, &$form_state) {

  // Unset destination first.
  unset($_GET['destination']);
  unset($_REQUEST['edit']['destination']);

  $form_state['redirect'] = 'FOO/BAR';
}
3
  • This works for redirects after adding a node, but not after editing it. I understand that the calls to unset the destination is supposed to handle the edit use case, but they have no effect on my Drupal 7 site. Commented Aug 16, 2018 at 10:25
  • @FreeRadical – You are right. I updated the answer to use hook_form_BASE_FORM_ID_alter instead, which targets both – add and edit.
    – leymannx
    Commented Aug 16, 2018 at 10:36
  • @FreeRadical – Additional info: Difference between create new and edit node in form alter.
    – leymannx
    Commented Aug 16, 2018 at 10:51
1

The documentation is not very clear on it, but you should modify $_GET['destination'] variable, not $_REQUEST or $form_state['redirect'].

1
  • 1
    Modifying $form_state['redirect'] works fine in Drupal 7...
    – Felix Eve
    Commented Feb 11, 2014 at 9:49
1

in Drupal 7 you can alter the node edit form to 'attach' your own submit function, in which you can alter form_state of the node delete confirmation form to include the 'destination' parameter... just did this in a custom theme i've been developing as a subtheme of the 'seven' admin theme...

Documentation for hook_form_FORM_ID_alter

You can add this hook to a template/theme, not just to modules.

so in your theme's template.php, add this form alter hook:

function yourtheme_node_delete_form_submit( &$form, &$form_state ) {
    $form_state[ 'redirect' ][ 1 ][ 'query' ][ 'destination' ] = "admin/content";
}

and then, voila. no longer redirected to homepage after a node is deleted.

0

Solution from here http://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7#comment-18139

<?php
  function FORM_NAME_form_submit($form, &$form_state) {
    $form_state['redirect'] = array(
      // $path
      'path/to/redirect/to',
      // $options
      array('query' => array('key' => 'value')),
      // $http_response_code
      302,
    );
  }  
?>
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.