How to redirect to the same page after adding new content?
shaynl - August 18, 2009 - 17:21
Hey,
I tried look for a module or tweak for that with no luck…
How I can change the default setting for crating content that when I click "save" it will stay on the same page (the add form page) instead of going the new node page.
Same thing for editing an existing node.
Any idea?
Thanks,
Shay

Save and Edit
Hi,
Sounds like http://drupal.org/project/addanother or http://drupal.org/project/submitagain would help with the first situation, and http://drupal.org/project/save_edit with the second.
Hey, thanks!!
It's working great when adding a new node with submitagain but not when edit a node… save_edit not doing the job as I need the users to stay on the same page (the add new node page). The user can see the new nods in a block on that page (using views). Is it possible to edit node within a block view?
p.s
This is a print screen of my "add new node page":
http://www.mypicx.com/uploadimg/604093315_08192009_1.jpg
You could write your own
You could write your own module! Here's how -- haven't tried this, but might work and you could tweak it:
this goes in a file called redirectedit.module (or whatever you want to call it) (put the php tag in the beginning --don't need ?> in the ending, it just shows here to get the code formatted in color, and it's not needed for coding best practice where it has a risk of messing your web site page up for some reason if left in there.)
(switch out mycontenttype for your content type)
I'm going to use hook_nodeapi because it does a lot of node things, see here http://api.drupal.org/api/function/hook_nodeapi/6.
<?php
// Implementation of hook_nodeapi. Redirect user to add new node after saving a new node or updating an existing node.
function redirectedit_nodeapi ($node, $op, $a3 = NULL, $a4 = NULL) {
if ($node->type == "mycontenttype") {
switch ($op) {
// Saving/Inserting a new node into database.
case "insert":
// Redirect to new node form at example.com/node/add/mycontenttype.
$_REQUEST['destination'] = "node/add/mycontenttype";
break;
// Updating an existing node in database.
case "update":
// Redirect to new node form at example.com/node/add/mycontenttype.
$_REQUEST['destination'] = "node/add/mycontenttype";
// OPTIONALLY Redirect back to edit page.
// $nid is the second argument in URL, e.g., example.com/node/100/edit
// node is arg(0), 100 is arg(1), and edit is arg(2)
//$nid = arg(1);
// Redirect back to example.com/node/100/edit
//$_REQUEST['destination'] = "node/" . $nid . "/edit";
break;
}
}
}
?>
this goes in a file called redirectedit.info
; $Id$
name = Redirect to edit
description = Redirect user to add new node after saving a new node or updating an existing node.
core = 6.x
version = 6.x-1.0
those files go in a folder called sites/all/modules/redirectedit
there you have your new module! I think this will work, not sure. Also there's a http://drupal.org/project/formblock module to put forms in blocks. Hope that helps.
Thanks!!
I tried it now and it works like a charm!
How I can add more content types to this script?
Excellent!switch out the
Excellent!
copy the if block and paste beneath the first if block (without opening and closing php tags) to look like this
<?php
if ($node->type == "mycontenttype") {
switch ($op) {
// Saving/Inserting a new node into database.
case "insert":
// Redirect to new node form at example.com/node/add/mycontenttype.
$_REQUEST['destination'] = "node/add/mycontenttype";
break;
// Updating an existing node in database.
case "update":
// Redirect to new node form at example.com/node/add/mycontenttype.
$_REQUEST['destination'] = "node/add/mycontenttype";
// OPTIONALLY Redirect back to edit page.
// $nid is the second argument in URL, e.g., example.com/node/100/edit
// node is arg(0), 100 is arg(1), and edit is arg(2)
//$nid = arg(1);
// Redirect back to example.com/node/100/edit
//$_REQUEST['destination'] = "node/" . $nid . "/edit";
break;
}
}
if ($node->type == "mycontenttype") {
switch ($op) {
// Saving/Inserting a new node into database.
case "insert":
// Redirect to new node form at example.com/node/add/mycontenttype.
$_REQUEST['destination'] = "node/add/mycontenttype";
break;
// Updating an existing node in database.
case "update":
// Redirect to new node form at example.com/node/add/mycontenttype.
$_REQUEST['destination'] = "node/add/mycontenttype";
// OPTIONALLY Redirect back to edit page.
// $nid is the second argument in URL, e.g., example.com/node/100/edit
// node is arg(0), 100 is arg(1), and edit is arg(2)
//$nid = arg(1);
// Redirect back to example.com/node/100/edit
//$_REQUEST['destination'] = "node/" . $nid . "/edit";
break;
}
}
?>
and switch out mycontentype for myothercontentype, repeat the copy for as many content types you need.
Hope that helps.
Easier way to do this
You only need to add new content types into the "if" line at the beginning.
<?php
if (($node->type == "mycontenttype") || ($node->type == "myothercontenttype")) {
switch ($op) {
// Saving/Inserting a new node into database.
case "insert":
// Redirect to new node form at example.com/node/add/mycontenttypes.
$_REQUEST['destination'] = "node/add/".$node->type;
break;
// Updating an existing node in database.
case "update":
// Redirect to new node form at example.com/node/add/mycontenttypes.
$_REQUEST['destination'] = "node/add/".$node->type;
// OPTIONALLY Redirect back to edit page.
// $nid is the second argument in URL, e.g., example.com/node/100/edit
// node is arg(0), 100 is arg(1), and edit is arg(2)
//$nid = arg(1);
// Redirect back to example.com/node/100/edit
//$_REQUEST['destination'] = "node/" . $nid . "/edit";
break;
}
}
?>
______________________
Grand Junction Design, LLC
Drupal Development for
Progressive Nonprofits
Thanks!
Thanks!