我对 drupal_render 有疑问(假设 drupal_render 是我得到我想要的东西的正确方法 - 请随时纠正我 =)。
我正在建立一个表格。由于 FAPI 不提供“表格”字段,我想自己制作一个。我的方法:使用 theme() 函数,特别是 theme('table', ...) 或 theme_table(),并用相应的表单字段填充它(打算稍后添加 AHAH 功能)。这迫使我使用 drupal_render 作为表格单元格的值,这会导致表单元素出现一些问题。
该表按年份收集了用户目前正在编辑的组织的员工人数。代码如下所示:
$form['employees'] = array(
'#type' => 'fieldset',
'#title' => t('Employees'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$employee_query = db_query("SELECT * FROM {employees} WHERE id_organisation = %d", $org['idoOrganisation']);
$employee = array();
while ($row = db_fetch_array($employee_query)) {
$employee[] = $row;
}
$header = array(
t('Year'),
t('Total'),
t('Internal'),
t('External'),
t('Aerospace')
);
$em_delta = 0;
$rows = array();
foreach($employee as $em_delta => $value) {
$form['employees'][$em_delta]['year'] = array(
'#title' => '',
'#type' => 'date_select', // Comes with the date module
'#date_format' => $format_year,
'#date_label_position' => 'within',
'#date_year_range' => '-50:+3',
'#default_value' => $value[$em_delta]['year'],
'#id' => 'edit-employees-' . $em_delta . '-year', // Allready a quickfix, since the form is rendered without id
'#name' => 'employees['.$em_delta.'][year]', // Same here
);
$form['employees'][$em_delta]['total'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value['total'],,
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-total',
'#name' => 'employees['.$em_delta.'][total]'
);
$form['employees'][$em_delta]['internal'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value[$em_delta]['internal'],
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-internal',
'#name' => 'employees['.$em_delta.'][internal]',
);
$form['employees'][$em_delta]['external'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value[$em_delta]['external'],
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-external',
'#name' => 'employees['.$em_delta.'][external]',
);
$form['employees'][$em_delta]['aero'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => $value[$em_delta]['aero'],
'#size' => 1,
'#id' => 'edit-employees-' . $em_delta . '-aero',
'#name' => 'employees['.$em_delta.'][aero]',
);
$rows[] = array(
drupal_render($form['employees'][$em_delta]['year']),
drupal_render($form['employees'][$em_delta]['total']),
drupal_render($form['employees'][$em_delta]['internal']),
drupal_render($form['employees'][$em_delta]['external']),
drupal_render($form['employees'][$em_delta]['aero']),
);
}
$form['employees']['table'] = array (
'#value' => theme('table', $header, $rows, array(), NULL)
);
以下是我遇到的问题:
- 表单元素的 ID 和名称属性为空。我在 drupal 网站上找到了一些东西,并对此感到满意(尽管我不明白),现在手动设置这些属性。
- 文本字段的默认值被忽略。字段为空。当我让 drupal_get_form 呈现该字段时,会显示 default_value。这里有人建议改为设置#value-property,但后来我又读到这是完全不同的东西,可能会导致问题。
- date_select-Field 未完整呈现。包装器在那里,但是选择字段出现在代码之外,就在表格之前(即它出现在代码中的位置)。
希望就是这样=)有人可以帮忙吗?我究竟做错了什么?