Community & Support

Redirect to custom page after creating a node with nid as a parameter

Hi everybody,

I spent huge amount of time searching net to find best solution. Finally, after understanding node.module, I`ve found out that form_submit return value could be used for redirection.

So, what I want to do is to redirect a user to custom page after node creation with nid as a parameter.
To do it, you should:
1. Create a wrapper submission function, which will call default one (node_form_submit), retrieve nid of created node and redirect to your_custom_page.

<?php
function your_module_redirect_submit($form_id, $form_values)
{   
   
$node_url = node_form_submit($form_id, $form_values);
   
dpr("Node URL:".$node_url);
    return
'your_custom_page/'.str_replace('node/','',$node_url);
}
?>

2. Replace default one with yours on form alter for needed_node_type.

<?php
function your_module_form_alter($form_id, &$form)
{
    if (
$form_id == 'needed_node_type_node_form')
    {                               
        unset(
$form['#submit']['node_form_submit']);   
       
$form['#submit']['your_module_redirect_submit'] = array();                       
    }
}
?>

Hope it will help somebody.

Comments

You can do it

You can do it with

<?php
function yourmodule_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  if (
$node->type == 'needed_node_type') {
    if (
$op == 'insert') {
     
drupal_goto('your_url/' . $node->nid);
    }
  }
}
?>

--
CK Ng | myFineJob.com

Beware

Be careful with this. If you drupal_goto() in hook_nodeapi w/op=insert, you might find (among other things), that your CCK fields values don't get saved...

This has been working well for me:

<?php
function yourmodule_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  if (
$node->type == 'needed_node_type') {
    if (
$op == 'insert') {
     
$_REQUEST['destination'] = 'your_url/'.$node->nid;
    }
  }
}
?>

To add to Ryan's comments, if

To add to Ryan's comments, if you use drupal_goto() in this way, you bypass the entire Node Access system, thereby rendering any access modules invalid.

Full-time freelancer, always looking for work.
jaypan.com (my portfolio)

Read The Fine Manual :~)

You could simply use the Form API's #redirect property.
________________________
"Creativity is knowing how to hide your resources" - Albert Einstein.

________________________
"Creativity is knowing how to hide your resources" - Albert Einstein.

He needs the nid, I don't

He needs the nid, I don't think you can get it in form_alter hence #redirect will not work.

--
CK Ng | myFineJob.com

Rules module

http://drupal.org/project/rules You can use Tokens in URL