php form foreach,php – Drupal Form API – 使用foreach循环来构建表单

我正在构建一个Drupal模块,使用管理表单将图标绑定到特定页面.放置在某个目录中的每个图像都需要输出,旁边的选择框显示所有主要链接标题.

我使用foreach循环构建了表单但是当我使用dpm($form)检查输出时;在_submit函数中,每个图像页面元素的#value总是等于为最后一个图像设置的值.

这是我的代码:

function titleicon_admin_settings() {

$settings = variable_get('titleicon_settings', $default);

//build an array of primary link titles

$primary_links_items = menu_primary_links();

foreach ($primary_links_items as $item) {

$title = $item['attributes']['title'];

$href = $item['href'];

$titles[$href] = $title;

}

//build array of icons

$directory = file_directory_path() . '/icons';

$mask = '(jpg|jpeg|gif|png|JPG|JPEG|GIF|PNG)';

$icons = file_scan_directory($directory, $mask);

foreach ($icons as $icon) {

$name = $icon->name;

$path = base_path() . $icon->filename;

$html = ''%20.%20%24path%20.%20'';

$default_value = $settings[$name]['page'];

$form[$name] = array(

'#type' => 'fieldset',

'#title' => $name,

);

$form[$name]['path_to_icon'] = array(

'#type' => 'value',

'#value' => $path,

);

$form[$name]['icon'] = array(

'#type' => 'markup',

'#value' => $html,

);

$form[$name]['page'] = array(

'#type' => 'select',

'#title' => t('Show icon on page'),

'#default_value' => $default_value,

'#description' => t('Choose which page to show icon on.'),

'#options' => $titles,

);

}

$form['submit'] = array(

'#type' => 'submit',

'#value' => t('Save'),

);

return $form;

}

解决方法:

这很有道理.如果您的字段声明如下:

$form[$name]['path_to_icon'] = array(

'#type' => 'value',

'#value' => $path,

);

然后为每个文件更新相同的变量 – ‘path_to_icon’.字段集键“$name”在这里无关紧要,因为它仅用于将表单字段分组在一起.

你需要使用更像的东西:

$form[$name]['path_to_icon_'.$name] = array(

'#type' => 'value',

'#value' => $path,

);

然后,您将在发布表单后获得多个值.

但是,说实话,我不会使用$name作为变量名的元素,你应该更喜欢自动递增$fid(文件表中的文件ID)或每个文件的任何其他唯一和SAFE标识符. .

标签:php,foreach,drupal,drupal-fapi

来源: https://codeday.me/bug/20190610/1210479.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值