By trogie on
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
and that should do the trick.
or append 'destination' as a parameter to your URL
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>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
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:
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.
taxonomy is not saved when use redirect
i have my own node type and vocabulary assigned to it.
I have code like this:
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).
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:
+1
+1 to described redirect solution for Drupal 7. Works as needed with no side-effects.
This is working in D6
Will not Work
Will not Work
Redirect loop...
It is working for me.
Did you try the code out? I'll walk you through with comments.
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:
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 .
node add form
Try to add destination to node add form
+1
+1
I have used the rules module
And totally recomend it.