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

发布于 2024-08-08 00:53:11 字数 1351 浏览 5 评论 0原文

我最初在另一个线程中开始这个问题,但是那个线程有点回答,现在我主要想知道如何指定另一个表单操作...我尝试使用下面的代码,但是输出时的表单操作仍然存在未更改,尽管查看 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;

I originally started this question in another thread, but that thread was sorta, kinda answered, and now I primarily want to know how to specify another form action... I tried using the code below, but the form action, when output, remains unchanged, although looking at the print_r($form), it's correctly changed... Why isn't it picking 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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∝单色的世界 2024-08-15 00:53:11

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 文件,然后只需在 .模块文件:

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

此函数是一个钩子,因为它的命名正确(即用模块名称替换“hook”一词),并且它与 hook_form_alter 的函数签名匹配(即它采用相同的函数签名)参数)。

然后只需在站点的管理中启用您的模块,钩子就可以发挥它的魔力。

请注意,hook_form_alter 采用对表单的引用;这允许您就地修改它。

hook_form_alter() is likely the way to go.
Here are some hopefully helpful links:

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

Modifying Forms in Drupal 5 and 6

hook_form_alter

EDIT: reply to comment #1 below:

How to implement hook_form_alter():

You must create a module (you cannot use template.php). It's easier than it looks.

For a module named "formstuff", you would create formstuff.info and formstuff.module and put them in either sites/all/modules or sites/yoursitename/modules. Set up the .info and .module files per the instructions, then just create the following function in your .module file:

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

This function is a hook because it is named properly (i.e. replace the word 'hook' with the name of your module), and it matches hook_form_alter's function signature (i.e. it takes the same parameters).

Then just enable your module in your site's admin and the hook should do it's magic.

Note that hook_form_alter takes a reference to the form; this allows you to modify it in-place.

懒的傷心 2024-08-15 00:53:11

您需要将 form_alter 函数放入模块中,然后使用 ifswitch 来检查表单 ID。如果表单 ID 是您想要更改的 ID,则为表单提供一个操作属性

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

You need to put the form_alter function in a module and then use either if or switch to check the form ID. If the form ID is the one you want to alter then give the form an action property

$form['someID'] = array(
'#action' => 'path/you/want',
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文