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.

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.

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

ckng’s picture

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

ryan_courtnage’s picture

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:

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

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.

Contact me to contract me for D7 -> D10/11 migrations.

iimitk’s picture

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

ckng’s picture

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

--
CK Ng | myFineJob.com

ibandyop’s picture

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