0

我正在将一组表单发送到模板,但如果我想呈现单个表单元素,我很难弄清楚如何打印开始和结束表单标签。

这是一个示例,可让您了解发送到主题功能的结构:

function mymodule_page_callback() {
  ...
  $i = 0;
  foreach ($widgets as $widget) {
    $forms[] = drupal_get_form('my_renderable_form_' . $i, $widget);
    $i++;
  }
  return theme('my_theme_function', array('forms' => $forms));
}

在我的模板中,我正在构建一个每行包含 1 个表单的表格。这是我可以使它工作的唯一方法:

$header = array('field 1', 'field 2', '');
foreach ($variables['forms'] as $form) {
  $row = array(
    drupal_render($form['field1']),
    drupal_render($form['field2']),
    // Manually set the closing form tag
    drupal_render($form['submit']) . drupal_render_children($form) . '</form>'
  );
  // Now drupal_render($form) to get the opening/closing form tags
  // and stuff it at the beginning of the 1st column.
  // This has to be done last so the rest of the form doesn't render with it.
  $row[0] = str_replace('</form>', '', drupal_render($form)) . $row[0];
  $rows[] = $row;
}
print theme('table', array('header' => $header, 'rows' => $rows);

是否有适当的方法来呈现每个表单的打开和关闭?

4

1 回答 1

-2

不要drupal_render在单个表单元素上使用,而是render在整个表单上使用:

$header = array('field 1', 'field 2', '');
foreach ($variables['forms'] as $form) {
  $rows[] = array(array('data' => render($form), 'colspan' => 3));
}
print theme('table', array('header' => $header, 'rows' => $rows);

您可能应该考虑使用tableselectForm API 元素。

theme_table编辑:更新了文档中指定的 $rows[] 分配。

于 2013-05-24T01:06:24.083 回答