带有附加参数的表单提交处理程序
对于某些要求,我需要传递附加信息来表单提交处理程序。在表单 api 中,同时定义自定义提交处理程序,因为
$additional_args = array();
$form['#submit'][] = 'my_submit_handler'
我希望将处理程序提交为
function my_submit_handler($form, &$form_state, $additional_args){
For some requirement I need to pass additional information to form submit handler. In form api, while defining custom submit handler as
$additional_args = array();
$form['#submit'][] = 'my_submit_handler'
I expect to submit handler as
function my_submit_handler($form, &$form_state, $additional_args){
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
提交处理程序由 drupal fapi 调用,因此您不能执行类似的操作。相反,您可以做的是将您需要的内容添加到
$form
或$form_state
中。通常的做法是:在表单中添加一个字段,输入 value 来存储该值。如果表单定义中有该值,请不要执行此操作。
这将在
$form_state['values']['store']
中提供。将值添加到
$form_state['storage']
,如果您希望将验证句柄中的变量传输到提交处理程序,则完成:The submit handler is called by the drupal fapi, so you can't do something like that. Instead what you can do, is to add what you need, either to the
$form
, or to the$form_state
. The usual approaches is to:Added a field to the form, type value to store the value. Don't do this if you have the value in the form definition.
This will be available in
$form_state['values']['store']
.Add the value to
$form_state['storage']
, done if you variables in your validation handle you want to transfer to your submit handler:Drupal 7:自定义参数通过 $form_state['build_info']['args'] 自动传播
这是在 http://api.drupal.org/api/drupal/ 中说的include!form.inc/function/drupal_get_form/7
例如:
然后在
...
//$form_state['build_info']['args']
是一个数组,在索引 0 处包含参数$myAdditionnalArg
的值...
Drupal 7: Custom arguments are automatically propagated troug $form_state['build_info']['args']
This is said in http://api.drupal.org/api/drupal/includes!form.inc/function/drupal_get_form/7
Ex:
Then in
...
//$form_state['build_info']['args']
is an array containing at index 0 the value of argument$myAdditionnalArg
...
正如 $form['#submit'] 和 $form['#validate'] 和 $ form['#process'] 不再支持自定义参数,将参数传递给提交处理程序集(如所示代码所示)的建议方法是使用类似于以下内容的代码:
处理程序将检索值作为 <代码>$form['#first_paramater']。
需要注意的是,代码可以使用不同的字符串来代替
#first_paramater
,但它必须以#
开头。通常不需要像代码那样设置提交处理程序,但在某些情况下是有必要的,例如更改另一个模块创建的表单,或者为表单中存在的每个提交按钮设置不同的提交处理程序。形式。
drupal_retrieve_form() 保存传递的参数到
$form['#parameters']
中的表单构建处理程序,其中包含:$form_id
$form_state
参数As reported in $form['#submit'] and $form['#validate'] and $form['#process'] no longer support custom parameters, the suggested way to pass parameters to a submission handler set as in the shown code is to use code similar to the following:
The handler would retrieve the value as
$form['#first_paramater']
.To notice that, instead of
#first_paramater
, the code can use a different string, but it must start with#
.Normally it's not necessary to set a submission handler like the code does, but there are some cases where it is necessary, like to alter a form created by another module, or to set a different submission handler for each of the submission buttons present in a form.
drupal_retrieve_form() saves the parameters passed to the form build handler in
$form['#parameters']
which contains:$form_id
$form_state