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

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

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

如何实现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接受对表单的引用;这允许您就地修改它.

相关文章

vue阻止冒泡事件 阻止点击事件的执行 &lt;div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些