欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

How do I alter the form submission handler? 修改 submit 对原来的submit 进行一些处理 有大用 有大大用

       
up vote14down votefavorite                            
3                            

For some reason I need to replace some characters (if exist) in every single textfields in any form just right before they stored in database. In other words I need a hook in which I could get all submitted data and replace their characters.
I found this solution:

function hook_form_alter($form, $form_state, $form_id) {
  $form['#submit'] = 'my_custom_submission_function'; //这个是修改 submit
}
                               

But this isn't a good idea since I have to take care of the rest of the submission progress too. I just need to replace some characters. Is there any hook to do this? What are the solutions?

                              
shareimprove this question                                            
edited Dec 31 '11 at 15:42                                                
                                                   
kiamlaluno                                                    
68k8100200                                                    
asked Dec 31 '11 at 11:33                                                
                                                   
Pedram Behroozi                                                    
2801515                                                    
 
       

2 Answers 正确答案                 

activeoldestvotes                    
       
up vote20down voteaccepted                            

$form['#submit'] stores an array of form submit handlers. With the code you provided you're overwriting it by adding only your own. To get the result you want you should add your submit handler as the first element in the array like this:


                               

array_unshift($form['#submit'], 'mymodule_custom_submission'); 
//在原来的submit的前面 再加个 submit 就可以添加功能了,这个就是放在原来的submit之前执行

$form['#submit'][] = 'mymodule_custom_submission'  
//这个就是放在原来的submit之后执行
                               

Getting the right values in the right forms and replacing some characters is probably a bigger issue in itself. You may want to narrow down the forms/fields you need to change to make sure you don't break anything.

shareimprove this answer                                            
answered Dec 31 '11 at 13:31                                                
                                                   
Dooshta                                                    
3,95311224                                                    
 
       
       
up vote1down vote                            

Perhaps it would be better to perform these modifications during validation instead of submission as suggested by Drupal (see form_set_value function at https://api.drupal.org/api/drupal/includes!form.inc/function/form_set_value/7).

So in your hook_form_alter you could even add it to the end of existing validation functions if you like:

$form['#validate'][] = 'mymodule_custom_validation';
 //这个是在原来的验证之后再增加的附加的功能 ,好像也是可能的
                           
shareimprove this answer



来自 http://drupal.stackexchange.com/questions/18641/how-do-i-alter-the-form-submission-handler




                                               

Do something after submit the form at hook_form_alter [closed]                                                    

                                                       

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?

                                                                              
shareimprove this question                                                                                            
edited Aug 5 '15 at 19:07                                                                                                
                                                                                                   
Adrian Cid Almaguer                                                                                                    
7,62741944                                                                                                    
asked Jul 22 '12 at 1:21                                                                                                
                                                                                                   
cobenash                                                                                                    
3941420                                                                                                    

closed as off-topic by kiamlaluno May 4 '15 at 12:43                                                                            

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced, was solved by a cache clear, or was a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers." – kiamlaluno

If this question can be reworded to fit the rules in the help center, please edit the question.                                                                            

 

 
   
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 ? – AyeshK Jul 22 '12 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 Jul 22 '12 at 8:24                                                                                            
1                                                                                                         
Well actually, hook_insert gets called after saving the node to the database.. – AyeshK Jul 22 '12 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 Jul 22 '12 at 12:24                                                                                            
                                                       

2 Answers 正确答案                                                                 

activeoldestvotes                                                                    
                                                       
up vote5down voteaccepted                                                                            

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)

shareimprove this answer                                                                                            
edited Jul 1 at 2:23                                                                                                
                                                                                                   
Mark                                                                                                    
13919                                                                                                    
answered Jul 23 '12 at 0:50                                                                                                
                                                                                                   
cobenash                                                                                                    
3941420                                                                                                    
 
6                                                                                                             
Change $form['actions']['submit']['#submit'][]='my_callback_functio‌n'; to $form['#submit'][]='my_callback_function'; rather. – Duncanmoo Feb 4 '14 at 11:45                                                                                                
                                                       
                                                       
up vote0down vote                                                                            

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.

shareimprove this answer


来自 http://drupal.stackexchange.com/questions/37719/do-something-after-submit-the-form-at-hook-form-alter                                                                                            




 


普通分类: