1

我正在研究一个 Drupal 6 模块,我想从我保存在数据库中的数据中生成一个表,每行中都有复选框。表格生成良好,但复选框未在表格中呈现,而是将其节点 ID 放在表格下方。请看下面的截图:

drupal 复选框

“21”是“Test Question 01”的节点id,“19”是“Test Question 02”的节点id。

我正在使用的代码(是的,它都在一个不理想的主题函数中。我计划在解决复选框问题后移动东西):

function theme_qt_assignment_questions_table($form) {
   // Get the questions from the database
   $db_results = db_query('SELECT {qt_questions}.nid, title, lesson, unit FROM node INNER JOIN {qt_questions} on {node}.nid = {qt_questions}.nid WHERE lesson = %d AND unit = %d', 
                     $form['#lesson'], $form['#unit']);

  // Define the headers for the table
  $headers = array(
    theme('table_select_header_cell'),
    array('data' => t('Title'), 'field' => 'title'/*, 'sort' => 'asc'*/),
    array('data' => t('Lesson'), 'field' => 'lesson'),
    array('data' => t('Unit'), 'field' => 'unit'),
  );

  while($row = db_fetch_object($db_results)) {
    $checkboxes[$row->nid] = '';

    $form['nid'][$row->nid] = array(
      '#value' => $row->nid
    );
    $form['title'][$row->nid] = array(
      '#value' => $row->title
    );
    $form['lesson'][$row->nid] = array(
      '#value' => $row->lesson
    );
    $form['unit'][$row->nid] = array(
      '#value' => $row->unit
    );
  }

  $form['checkboxes'] = array(
    '#type' => 'checkboxes',
    '#options' => $checkboxes,
  );

  // Add the questions to the table
  if(!empty($form['checkboxes']['#options'])) {
    foreach(element_children($form['nid']) as $nid) {
      $questions[] = array(
        drupal_render($form['checkboxes'][$nid]),
        drupal_render($form['title'][$nid]),
        drupal_render($form['lesson'][$nid]),
        drupal_render($form['unit'][$nid]),
     );
    }
  } else {
    // If no query results, show as such in the table
    $questions[] = array(array('data' => '<div class="error">No questions available for selected lesson and unit.</div>', 'colspan' => 4));
  }

  // Render the table and return the result
  $output = theme('table', $headers, $questions);

  $output .= drupal_render($form);
  return $output;
}
4

1 回答 1

0

事实证明,我试图简化问题实际上是问题所在。也就是说,在 hook_theme 中做所有事情都是不正确的。相反,我定义了一个从数据库中提取信息并创建复选框数组并在 hook_form 中调用它的函数:

$form['questions_wrapper']['questions'] = _qt_get_questions_table($node->lesson, $node->unit);

在这个函数(_qt_get_questions_table())的末尾,我指定了将所有内容都放入表中的主题函数:

  $form['#theme'] = 'qt_assignment_questions_table'; 

我对 Drupal 还是很陌生,所以这个解释对于有同样问题的人来说可能不是最好的,但希望它会有所帮助。

于 2012-05-13T03:04:12.557 回答