我最初在另一个线程中启动了这个问题,但该线程有点,有点回答,现在我主要想知道如何指定另一个表单动作…我尝试使用下面的代码,但表单操作,当输出时,仍然没有改变,虽然看了print_r($form),它正确地改变了……为什么它没有恢复?
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;
解决方法
hook_form_alter()
可能是要走的路.
以下是一些有用的链接:
Form Theming: How do I set $form[‘action’]?
Modifying Forms in Drupal 5 and 6
如何实现hook_form_alter():
您必须创建一个模块(不能使用template.PHP).它看起来比它看起来容易.
对于名为“formstuff”的模块,您将创建formstuff.info和formstuff.module并将它们放在sites / all / modules或sites / yoursitename / modules中.根据the instructions设置.info和.module文件,然后在.module文件中创建以下函数:
function formstuff_form_alter(&$form,$form_state,$form_id) { // do stuff }
这个函数是一个钩子,因为它被正确命名(即用你的模块名称替换’hook’这个词),它匹配hook_form_alter的函数签名(即它采用相同的参数).
然后只需在您网站的管理员中启用您的模块,钩子应该是魔术.
请注意,hook_form_alter接受对表单的引用;这允许您就地修改它.