Drupal Form API - 使用 foreach 循环构建表单
我正在构建一个 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;
}
I'm building a Drupal module to tie an icon to a particular page using an administration form. Each image placed within a certain directory needs to be output with a select box next to it showing all the primary link titles.
I've built the form using a foreach
loop but when I check the output using dpm($form);
in the _submit function the #value for each images page element is always equal to what ever is set for the last image.
Here's my code:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是完全有道理的。如果您的字段声明如下:
那么对于每个文件,您将更新相同的变量 - 'path_to_icon'。字段集键“$name”在这里并不重要,因为它仅用于将表单字段分组在一起。
您需要使用类似的东西:
那么您在发布表单后将获得多个值。
然而,说实话,我不会使用 $name 作为变量名的元素,你应该有类似自动递增 $fid (文件表中的文件 id)或每个文件的任何其他唯一且安全的标识符之类的东西。 。
Which makes perfect sense. If your fields are declared like this:
then for each file you are updating the same variable - 'path_to_icon'. Fieldset key "$name" does not matter here, as it is used only for grouping form fields together.
You would need to use something more like:
then you will get multiple values after posting the form.
However, to tell the truth, I wouldn't use $name as element of variable name, you should rather have something like auto-incrementing $fid (file id from files table) or any other unique and SAFE identifier for each file...
我必须说,如果您将“#tree => TRUE”放入字段集的声明中:
则不必在所有表单元素中放入“_'.$name”。 Drupal 会将所有表单结果分组到以 $name 为键的数组中。
I must say that if you put "#tree => TRUE" in the declaration of your fieldset :
you don't have to put "_'.$name" in all your form elements. Drupal will group all the form results in arrays keyed by $name.