php – Drupal 7 FAPI-在验证或提交处理程序中添加表单元素
作者:互联网
是否可以在drupal 7模块的验证或提交函数中添加其他表单元素?以下代码工作,图像显示在窗体上:
function test1_form($form, &$form_state)
{
$form['image']=array(
'#markup'=>'<img src="sites/all/modules/create_ad/th.jpeg"><br/>', //replace with your own image path
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
}
但是当我尝试在提交函数中提交后显示图像,如下所示,它不起作用:
function test1_form($form, &$form_state)
{
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
}
function test1_form_submit($form,&$form_state)
{
$form['image']=array(
'#markup'=>'<img src="sites/all/modules/create_ad/th.jpeg"><br/>', //replace with your own image path
);
}
欢迎任何积极的帮助.谢谢.
解决方法:
您可以按照多步骤表单方法在提交后向表单添加其他字段.
Here is a blog post讨论了一个多步骤的方法,可以给你一些见解.
基本上,在提交函数中,您可以存储值并设置要重建的表单.然后在表单函数中检查这些存储的值并添加新字段(如果存在).
例:
<?php
function test1_form($form, &$form_state)
{
if (isset($form_state['storage']['show-image'])){
$form['image']=array(
'#markup'=>'<img src="sites/all/modules/create_ad/th.jpeg"><br/>', //replace with your own image path
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
}
function test1_form_submit($form,&$form_state)
{
$form_state['rebuild'] = TRUE;
$form_state['storage']['show-image'] = true;
}
标签:drupal-forms,drupal-modules,php,drupal-7,drupal-fapi 来源: https://codeday.me/bug/20190725/1537049.html