【发布时间】:2011-06-23 15:31:53
【问题描述】:
我正在构建一个 Drupal 模块,以使用管理表单将图标绑定到特定页面。放置在某个目录中的每个图像都需要输出,旁边有一个选择框,显示所有主链接标题。
我使用foreach 循环构建了表单,但是当我在_submit 函数中使用dpm($form); 检查输出时,每个图像页面元素的#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 = '<img src="' . $path . '" width="50" height="50" />';
$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;
}
【问题讨论】:
标签: php drupal foreach drupal-fapi