Search code examples
drupaldrupal-6drupal-theming

Drupal 6: Checkboxes table not rendering properly


I'm working on a Drupal 6 module which I want to generate a table with checkboxes in each row from data I have saved in a database. The table is being generated fine, but the checkboxes are not rendering in the table but are instead having their node id's put below the table. See the screenshot below:

drupal checkboxes

"21" is the node id of "Test Question 01", and "19" is the node id of "Test Question 02".

The code I'm using (yes, it is all in a theme function which isn't ideal. I'm planning on moving stuff around once the checkboxes problem is resolved):

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

Solution

  • Turns out my attempt at simplification of the problem was, in fact, the problem. Namely, doing everything in hook_theme isn't correct. Rather, I defined a function that pulls the info from database and creates the checkboxes array and call it in hook_form as such:

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

    At the end of this function (_qt_get_questions_table()), I specify the theme function which put everything into the table as such:

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

    I'm still very new to Drupal so this explanation may not be the best to someone having the same problem, but hopefully it will help.