How to override form action in Drupal?

I initially started this question in another thread, but this thread was sorted, somehow answered, and now I first want to know how to specify a different form action ... I tried to use the code below, but the form action, when the output, remains unchanged, although looking at print_r($form) , it changed correctly ... Why doesn't it go up?

 function mytheme_user_profile_form($form) { global $user; $uid = $user->uid; //print '<pre>'; print_r($form); print '</pre>'; $category = $form['_category']['#value']; switch($category) { case 'account': $form['#action'] = '/user/'.$uid.'/edit?destination=user/'.$uid; break; case 'education': $form['#action'] = '/user/'.$uid.'/edit/education?destination=user/'.$uid; break; case 'experience': $form['#action'] = '/user/'.$uid.'/edit/experience?destination=user/'.$uid; break; case 'publications': $form['#action'] = '/user/'.$uid.'/edit/publications?destination=user/'.$uid; break; case 'conflicts': $form['#action'] = '/user/'.$uid.'/edit/conflicts?destination=user/'.$uid; break; } //print '<pre>'; print_r($form); print '</pre>'; //print $form['#action']; $output .= drupal_render($form); return $output; 
+4
source share
2 answers

hook_form_alter() is most likely the way to go. Here are some useful links:

Form Theming: How to set $ form ['action']?

Changing forms in Drupal 5 and 6

hook_form_alter

EDIT: answer to comment # 1 below:

How to implement hook_form_alter () :

You must create a module (you cannot use template.php ). This is easier than it sounds.

For a module named "formstuff", you must create formstuff.info and formstuff.module and put them in sites/all/modules or sites/yoursitename/modules . Configure the .info and .module files for the instruction , and then simply create the following function in the .module file:

 function formstuff_form_alter(&$form, $form_state, $form_id) { // do stuff } 

This function is a hook, because it is correctly named (that is, it replaces the word "hook" with the name of your module) and corresponds to t27> (that is, it takes the same parameters).

Then just plug your module into your site admin and the hook should do the magic.

Note that hook_form_alter refers to the form; this allows you to change it in place.

+7
source

You need to put the form_alter function in the module, and then use if or switch to check the form identifier. If the form identifier is the one you want to change, then give the form an action property

 $form['someID'] = array( '#action' => 'path/you/want', ); 
+1
source

All Articles