与无线电的Drupal表

The*_*act 3 forms drupal radio-button tabular

如何将'radios'添加到围绕theme_table构建的表单中?

使用单个"复选框"或单个"收音机"似乎工作正常,但是一旦我使用"无线电",就没有任何单选按钮渲染.

从另一个Stack Overflow问题,我已经看过form_process_radios()提到过,使用它实际上显示了单选按钮.但它们不再捆绑在一起,所有这些都可以立即进入"开启"状态.

有任何想法吗?

ber*_*kes 7

简单回答:你不能轻易.(尽管你可能已经成功定义了自己的处理器,使用expand_radios,硬核东西!).

更长的答案:radios使用theme_radios.正如您所看到的,它使用单个DIV包装器,这使得无法在桌面上展开无线电.

你最能做的是创建一个分层表单,radio每个选项一个.避免radios.通过分层次地对它们进行分组,name将是相同的,这是无线电被分组的方式.

# from install.php:
  foreach ($names as $profile => $name) {
    $form['profile'][$name] = array(
      '#type' => 'radio',
      '#value' => 'default',
      '#return_value' => $profile,
      '#title' => $name,
      '#description' => isset($profiles[$profile]['description']) ? $profiles[$profile]['description'] : '',
      '#parents' => array('profile'),
    );
  }
Run Code Online (Sandbox Code Playgroud)

然后,在围绕表单构建表的主题函数中,您将在适当的表格单元格中呈现每个单元格.


小智 6

因此,要使用Berkes建议,您必须使用'#atributes'属性,同一组的所有无线电元素具有相同的名称.

像这样:

'#attributes' => array('name' => array('somename'))
Run Code Online (Sandbox Code Playgroud)

这是一个更完整的例子:

for ($i = 0; $i < $num; $i++){
  $element[$i]['answer'] = array(
    '#type' => 'textfield',
    '#title' => t('Answer'),
  );
  $element[$i]['correct'] = array(
      '#type' => 'radio',
      '#value' => 'default',
      '#return_value' => $i,
      '#title' => t('Correct answer'),
      '#attributes' => array('name' => array('correct-answer')),
  );
}
Run Code Online (Sandbox Code Playgroud)