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

nevets’s picture

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.

orangutangle’s picture

You can also link to the node add form appending the destination parameter to the URL, which effectively does the same thing without a hook. For example:
<a href="node/add/content-type?destination=/path/to/destination">Add new content</a>

carlmcdade’s picture

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

trogie’s picture

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

mooffie’s picture

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

trogie’s picture

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

robloach’s picture

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

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.

mooffie’s picture

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.

john.karahalis’s picture

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?

enboig’s picture

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?

tun@sun’s picture

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

mshepherd’s picture

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.

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;
  }
}
Arek’s picture

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

I have code like this:

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

Odaeus’s picture

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.

zilverdistel’s picture

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).

$_GET['destination'] = 'node/nid/edit';

worked though ..

fenstrat’s picture

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:

function hook_node_insert($node) {
  $_GET['destination'] = 'node/' . $node->nid . '/edit';
}
johnhanley’s picture

+1 to described redirect solution for Drupal 7. Works as needed with no side-effects.

saltednut’s picture

function mymodule_nodeapi(&$node, $op, $a3, $a4) {
  if($node->type == 'content_type'){
      if($op == 'insert'):
          $_REQUEST['destination'] = 'node/add/content-type';  
      endif;
  }
Nikdhil Mdohfan’s picture

Will not Work
Redirect loop...

saltednut’s picture

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

//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:

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...

Nikdhil Mdohfan’s picture

Your taxonomy terms may not get saved .

dotpex’s picture

Try to add destination to node add form

Nikdhil Mdohfan’s picture

+1

fedegf’s picture

And totally recomend it.