如何在Drupal中覆盖表单操作?

n00*_*101 4 forms action drupal drupal-fapi drupal-alter

我最初在另一个线程中启动了这个问题,但该线程有点,有点回答,现在我主要想知道如何指定另一个表单动作...我尝试使用下面的代码,但表单操作,当输出时,仍然没有改变,虽然看着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;
Run Code Online (Sandbox Code Playgroud)

mik*_*ers 7

hook_form_alter()可能是要走的路.以下是一些有用的链接:

表格主题:如何设置$ form ['action']?

修改Drupal 5和6中的表单

hook_form_alter

编辑:回复下面的评论#1:

如何实现hook_form_alter():

您必须创建一个模块(您不能使用template.php).它看起来比它看起来容易.

对于一个名为"formstuff"模块中,您将创建formstuff.infoformstuff.module把他们在任一sites/all/modulessites/yoursitename/modules.成立了.info和.module文件每个指令,然后就在你的.module文件创建以下功能:

function formstuff_form_alter(&$form, $form_state, $form_id) {
  // do stuff
}
Run Code Online (Sandbox Code Playgroud)

这个函数是一个钩子,因为它被正确命名(即用模块名称替换'hook'一词),它匹配hook_form_alter的函数签名(即它采用相同的参数).

然后只需在您网站的管理员中启用您的模块,钩子应该是魔术.

注意,hook_form_alter参考表格; 这允许您就地修改它.