Community

Redirect to other page instead of 'view' after node creation/edit.

Hello,

I'm writing a module with a custom content type that has an extra tab (MENU_LOCAL_TASK). After creating a new node and/or editing one, I would like drupal to swith automatically to that new tab instead of to the view page.

What is the best way to do that?

Trogie

Comments

Set $_REQUEST['destination'] to your path

In your submit hook (or you can add it to the insert and update hooks), add a line like

$_REQUEST['destination'] = "The Path To Your Tab"; 

and that should do the trick.

You can also

You can also try

http://api.drupal.org/api/5/file/developer/topics/forms_api_reference.ht...

and using hook_form_alter

Hiveminds Magazine
http://www.hiveminds.co.uk
for web publishers and community builders CMS Demo Matrix
Coming soon!Drupal Support | Drupal S

Drupal Web Developer - Stockholm Sweden
Drupal Sweden

I like this way

Hello,

I like this 'drupal' way to redirect but I have to specify this $form['#redirect'] array before the node gets created and thus dont know the nid yet...

So I can not create a redirect url to '/node/' . $nid . '/extra_tab/' ?

This system works for editing.

Trogie

...

So do what nevets suggests, in hook_insert. You know the nid at this stage.

OK, Hook_insert probably

OK,

Hook_insert probably doesn't get executed because my module is not really a custom content type but a content-type extension... Anyway, I'll have to use nodeapi then and filter node insertions for nodes with the extension enabled.

Thanks for helping me out,
Trogie

Using hook_nodeapi

Here's a way to redirect the user when a node is created by using the hook_nodeapi and drupal_goto:

<?php
function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch (
$op) {
    case
'submit':
      if (!
$node->nid && $node->type == 'mynodetype') {
       
drupal_set_message(t('%nodetitle has been created.', array('%nodetitle' => $node->title)));
       
node_save($node);
       
drupal_goto('node/add/mynodetype');
       
      }
      break;
  }
}
?>

You'll have to make your own module (replace 'mymodule' with the module's name), and then replace 'mynodetype' with what node you want to have redirected.

...

Messing with Drupal's internals is not a wise thing. You did this 'drupal_set_message' call because your code shortcuts the Drupal code which does it. You had to examine 'node.module' to figure out what to implement yourself that Drupal will no longer do (because of the 'drupal_goto' call which trerminates the script). But what about future versions of Drupal? Your code is not guaranteed to work. (And it's breakable as it is.)

I would replace the three lines inside the 'if' statement with the line nevets gave.

Drupal 6

How about for Drupal 6? I would expect that most of the code would be the same, but want to be sure.

Also, what is the name of the internal Drupal file that must be modified?

how about drupal6?

I tried using nevets solution, writing $_REQUEST in hook_insert but it didn't work; am I missing something or in drupal6 it has to be made another way?

La vida és una taronja, què esperes per exprimir-la?

for drupal6

using nodeapi
$op will be 'insert' instead of 'submit'

a d6 example

An example in D6.

Here, an anonymous user will be redirected to the home page on node submission. Other users will redirect back to the node creation page.

<?php
function mymodulename_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  global
$user;
  switch (
$op) {
    case
'insert':
      if (
$node->type == 'h_energy_feedback' && $user->uid == 0) {
       
drupal_set_message(t('Your feedback has been saved. Thanks for taking the time!'));
       
drupal_goto();
      } else {
       
drupal_set_message(t('The feedback was saved.'));
       
drupal_goto('forms/henergy-feedback');
      }
      break;
  }
}
?>

taxonomy is not saved when use redirect

i have my own node type and vocabulary assigned to it.

I have code like this:

<?php
function mymodule_insert {
  
//my other code here
  
drupal_goto('my_page');
}
function
mymodule_update {
  
//my other code here
  
drupal_goto('my_page');
}
?>

when it is submitted or updated taxonomy is not saved (there is no new record or record update in term_node table). anybody know how to fix it? i am using drupal 6

Can't use drupal_goto in insert or update

As I've just discovered, drupal_goto() sends a redirect instantly instead of waiting to get to the content response stage of processing. This means that putting it in hook_insert or hook_update functions will stop other hooks from getting called. You need to alter the form redirect or use the hacky $_REQUEST['destination'] method. I had to do the latter as I needed the node ID in the URL.

Drupal 7

In my situation, I also needed the nid. Setting the $_REQUEST['destinitation'] didn't work though, probably because the destination parameter was allready set in the url (node/add/mytype?destination=admin/content/node).

<?php
$_GET
['destination'] = 'node/nid/edit';
?>

worked though ..

Drupal 7, use $_GET

As noted by @zilverdistel in D7 setting the $_GET['destination'] will override the destination after node add.

E.g. Redirect to node edit after add:

<?php
function hook_node_insert($node) {
 
$_GET['destination'] = 'node/' . $node->nid . '/edit';
}
?>

This is working in D6

<?php
function mymodule_nodeapi(&$node, $op, $a3, $a4) {
 if($node->type == 'content_type'){
     if($op == 'insert'):
         $_REQUEST['destination'] = 'node/add/content-type';  
     endif
;
 }
?>

Will not Work

Will not Work
Redirect loop...

Nikhil Mohan
skype : nikhilmkumar
BBM PIN : 31010B49

It is working for me.

Did you try the code out? I'll walk you through with comments.

<?php
//use nodeapi hook, prefix with the name of your module
function mymodule_nodeapi(&$node, $op, $a3, $a4) {
 
//check which type of content we are looking at
  //in this example, it is using the machine name "content_type"
 
if($node->type == 'content_type'){
   
//make sure we are on the 'insert' state for $op
    //this only happens once while a new node is being created
    //if you are creating nodes via batch, this is not a good idea
   
if($op == 'insert'){
        
//redirect the user
       
$_REQUEST['destination'] = 'node/add/content-type'
    }
  }
}
?>

Just tested this out one more time and its still working for me - which is good since we are using it on a production site!

Care to explain why this working code will create a redirect loop? Perhaps you did not read the code correctly.

If one were to say:

<?php
function mymodule_nodeapi(&$node, $op, $a3, $a4) {
  if(
$node->type == 'content_type'){
   
$_REQUEST['destination'] = 'node/add/content-type'
  }
}
?>

Then yes, that would surely redirect in a loop on the edit view. The caveat is checking for an "insert" action from the $op argument.

See: http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hoo...

Your taxonomy terms may not

Your taxonomy terms may not get saved .

Nikhil Mohan
skype : nikhilmkumar
BBM PIN : 31010B49

node add form

Try to add destination to node add form

+1

+1

Nikhil Mohan
skype : nikhilmkumar
BBM PIN : 31010B49