2

I want to do something after I submit the node. How can I do that?

I use hook_form_form_id_alter(),but I dont know how to use callback function called after the user submit the form

function mymodule_form_node_form_alter(&$form,&$form_state,$form_id)
{
     if($form['#node']->type=="my_content_type")
     {
          $form['actions']['submit']['#submit'][]=my_callback_function;
     }
}

function my_callback_function($form,$form_state)
{
     //my code here!!
     //execute after the user submit the node form
     //and then redirect user to another place,after executing the code above

}

it doesn't work! and I don't know why? Please tell me how can I do that?

4
  • What is your Drupal version ? Can you confirm that your code is executed ? Put drupal_set_message('worked'); in the form alter function. Is there any reason why don't you use hook_node_insert/hook_nodeapi ?
    – AKS
    Commented Jul 22, 2012 at 7:50
  • my drupal version 7.x; I need to do something and then redirect user to the page that i want. But i cant use drupal_goto in the hook_node_inser. So i need to use form['#redirect']. Are there any method that i can fix this problem?
    – cobenash
    Commented Jul 22, 2012 at 8:24
  • 1
    Well actually, hook_insert gets called after saving the node to the database..
    – AKS
    Commented Jul 22, 2012 at 10:37
  • yes, i know that, but i can't call drupal_goto. i need to redirect user to the page i want. Can you help me? Thank you very much
    – cobenash
    Commented Jul 22, 2012 at 12:24

2 Answers 2

7

Sorry about that! My problem is fixed.

the code belowed is the anwser

function mymodule_form_node_form_alter(&$form,&$form_state,$form_id)
{
     if($form['#node']->type=="my_content_type")
     {
          $form['actions']['submit']['#submit'][]='my_callback_function';
     }
}

function my_callback_function($form, &$form_state)
{
     //my code here!!
     //execute after the user submit the node form
     //and then redirect user to another place,after executing the code above

}

[Edit] For me, the callback would not fire unless form_state was declared as &$form_state (pass by reference)

1
  • 6
    Change $form['actions']['submit']['#submit'][]='my_callback_function'; to $form['#submit'][]='my_callback_function'; rather.
    – Duncanmoo
    Commented Feb 4, 2014 at 11:45
0

For other that are struggling with a similar issue. You could use rules if you need to redirect after form submission. Rules can react on form events, and you can easily attach the actions you want to it.

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