0

I find some strange problem. I use hook_node_insert(). In order to create the other node after the user create the specific node! Then, I redirect the user to the other pages But there are some error messages that i can't figure out!

the below code is my hook_node_insert()

function campaign_node_insert($node)
{
    global $user;
    global $base_url;

    if($node->type=="my_content_type")
    {

            $userid=$user->uid;

        /**

                My code is right here !


               **/


               drupal_goto('/user/'.$userid);


          }
}

My error Message is as belowed :

enter image description here

2 Answers 2

3

You can't use drupal_goto() in hook_node_insert() and other node_hooks too. The reason is that there is a queue of hooks that are executed on node insert and if you run drupal_goto() in some hook - all other hooks which goes after will not execute!

If you need redirect user after node insert to some url - implement hook_form_alter for node_form and add new submit callback $form['actions']['submit']['#submit'][] = 'my_module_submit_callback'. In this callback you will get $form_state with all $node fields and you can perform you redirect by specifying $form['#redirect'] = 'some/url'.

4
  • oh~i see, thank you very much! i almost make a big mistake!
    – cobenash
    Commented Jul 21, 2012 at 9:41
  • Another point is that, even if it would work, it would also be executed for nodes that are created through other means than the UI. For example a migration, feeds or some other background process.
    – Berdir
    Commented Jul 21, 2012 at 9:44
  • Hello,i use $form['actions']['submit']['#submit'][]=campaign_add_record_callback; and create function campaign_add_record_callback doesn't work. :( the error message shows that "campaign_add_record_callback " is an undefined constant
    – cobenash
    Commented Jul 21, 2012 at 10:42
  • Cobenash, you have forgotten quotes around function name, try $form['actions']['submit']['#submit'][]='campaign_add_record_callback'; Commented Jul 21, 2012 at 10:48
0

Eugene is correct, although for D7 your submit handler should contain $form_state['redirect'] = 'some/url'.

You can look at the node_form_submit() function to see how Drupal core handles this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.