从自定义 Drupal 表单访问 Drupal 的 $form_values 中的值

发布于 2024-11-15 13:01:03 字数 1316 浏览 1 评论 0原文

编辑:看来我的“数组爬行”技能还不够,谢谢你的建议。 此外,我发现我正在用普通的“=”而不是双“==”检查 $discounttype 条件。我猜你在同一个代码块上连续敲打 3 个小时会让你变得愚蠢并错过最明显的错误。

首先,我使用 Drupal 6。

我创建了一个带有以下标记的表单:

$form["cart_".$index] = array(
'#type' => 'image_button',
'#src'=> 'files/imghome/sidebar-add-demo.gif',
'#attributes' => array('rel' => '#item', 'class' => 'buybutton', 'title' => $discounttype),
'#prefix'=>'<p class="renewprop">'.$newren.' for '.$node_abb->field_tipo_abb_value.':</p><p class="renewblock"><span class="pricetag">'.$node_abb->field_prezzo_value.''.$discounttype.'</span>',
'#suffix' =>'</p>' ,
'#submit' =>array('usercp_form_submit'),
);

表单渲染正确,正如您从这张图片中看到的: http://cl.ly/3D2C2h1t1m2B351L1T31 (€ 符号旁边的 N 和 R 值实际上是 $discounttype 变量的值,只是为了检查它)

每个白框基本上都是上述形式的一个实例。

我需要在每次提交时传递 $discounttype 变量的值,因此我决定将其设置为提交按钮的标题。

我的问题是,在提交函数本身中,我无法访问 #attributes 数组中包含的“title”属性的值。主要是因为我可能不知道正确的语法。

到目前为止,我已经尝试过

$foo = $form_values['attributes']['title'];
$foo = $form_values['#attributes']['title'];
$foo = $form_values['attributes']['#title'];

以及所有其他可能的组合,但可能我只是做错了。 实际上,我花了一个小时在网上爬行寻找答案,但我想出了任何东西。

EDIT: Seems like my "array-crawling" skills were not enough, thanks for the suggestions.
Moreover, I found out that I was checking the $discounttype condition with a plain "=" instead of a double "==". I guess banging your head on the same block of code for 3 hours makes you dumb and miss the most obvious errors.

First thing first, I'm on Drupal 6.

I have created a form with the following markup:

$form["cart_".$index] = array(
'#type' => 'image_button',
'#src'=> 'files/imghome/sidebar-add-demo.gif',
'#attributes' => array('rel' => '#item', 'class' => 'buybutton', 'title' => $discounttype),
'#prefix'=>'<p class="renewprop">'.$newren.' for '.$node_abb->field_tipo_abb_value.':</p><p class="renewblock"><span class="pricetag">'.$node_abb->field_prezzo_value.''.$discounttype.'</span>',
'#suffix' =>'</p>' ,
'#submit' =>array('usercp_form_submit'),
);

The form renders correctly, as you can see from this picture: http://cl.ly/3D2C2h1t1m2B351L1T31
(the N and R values beside the € symbol are actually the value of the $discounttype variable, just for checking it)

Each white box is basically an istance of the beforementioned form.

I need to pass the value of the $discounttype variable on each submit, so I decided to set it as the title of the submit button.

My problem is that in the submit function itself I cannot access the value of the 'title' attribute contained in the #attributes array. Mainly because probably I don't know the right syntax.

So far I've tried

$foo = $form_values['attributes']['title'];
$foo = $form_values['#attributes']['title'];
$foo = $form_values['attributes']['#title'];

And every other possible combination, but probably I'm just doing it wrong.
It's actually an hour that I'm crawling the web searching for an asnwer but I came up with anything.

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

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

发布评论

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

评论(2

夏有森光若流苏 2024-11-22 13:01:03

首先,您应该提及表单元素 ID。
因此,您可以通过 $form_state["cart_".$index]['#attributes']['title']; 访问提交按钮
但实际上,为什么不使用隐藏字段 ('#type' => 'hidden') ?

first, you should mention form element ID.
so, you can access submit button by $form_state["cart_".$index]['#attributes']['title'];
but actually, why don't you use hidden field ('#type' => 'hidden') ?

吖咩 2024-11-22 13:01:03

我相信您必须使用 $form_state 而不是 $form_values。尝试一下:

$foo = $form_state['clicked_button']['#attributes']['title'];

我建议在 Drupal 开发时使用 Devel 模块。它是开发过程中非常有用的工具,允许您查看页面加载时运行的所有查询、停止重定向以进行调试等等更多

I believe you have to use $form_state instead of $form_values. Give this a try:

$foo = $form_state['clicked_button']['#attributes']['title'];

I recommend using the Devel module while developing for Drupal. It is an extremely helpful tool during development, allowing you to see all the queries run when a page loads, stop a redirect to debug, and much more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文