Drupal 6:Checkboxes表无法正确呈现

时间:2012-05-11 23:36:30

标签: drupal drupal-6 drupal-theming

我正在研究一个Drupal 6模块,我希望生成一个表,每行都有一个复选框,来自我保存在数据库中的数据。该表生成正常,但复选框不在表中呈现,而是将其节点ID放在表下方。请参见下面的屏幕截图:

drupal checkboxes

“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;
}

1 个答案:

答案 0 :(得分: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来说还是一个新手,所以这个解释对于遇到同样问题的人来说可能不是最好的,但希望它会有所帮助。