如何在 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;
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
hook_form_alter()
可能是可行的方法。以下是一些希望有用的链接:
表单主题:如何设置 $form['action']?
在 Drupal 5 和 6 中修改表单
hook_form_alter
编辑:回复下面的评论 #1:
如何实现 hook_form_alter():
您必须创建一个模块(不能使用
template.php
)。这比看起来容易。对于名为“formstuff”的模块,您可以创建
formstuff.info
和formstuff.module
并将它们放入sites/all/modules
或sites/yoursitename/modules
。按照说明设置 .info 和 .module 文件,然后只需在 .模块文件:此函数是一个钩子,因为它的命名正确(即用模块名称替换“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
andformstuff.module
and put them in eithersites/all/modules
orsites/yoursitename/modules
. Set up the .info and .module files per the instructions, then just create the following function in your .module file: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.您需要将
form_alter
函数放入模块中,然后使用if
或switch
来检查表单 ID。如果表单 ID 是您想要更改的 ID,则为表单提供一个操作属性You need to put the
form_alter
function in a module and then use eitherif
orswitch
to check the form ID. If the form ID is the one you want to alter then give the form an action property