Drupal theme table "bug"

I happen to find another bug with Drupal theme table functions. Before reading further, make sure you know about theme table function. In cell information, you can pass use an array with header key to indicate that this cell is a header cell (TH tag). So the following code will generate a pair of TH & TD tags for you each rows.

$rows = array(
array(
array('data' => 'Col1','header' => true),
array('data' => 'Col2'),
),
array(
array('data' => 'Col1','header' => true),
array('data' => 'Col2'),
),
);

$output = theme('table', array(), $rows);

The generated HTML will be something like this:

Col1Col2
Col1Col2

But take look at the below code:

$rows = array(
array(
array('data' => 'Col1','header' => true),
array('data' => 'Col2','header' => false),
),
array(
array('data' => 'Col1','header' =>; true),
array('data' => 'Col2','header' => false),
),
);

$output = theme('table', array(), $rows);

It's logical if you use TRUE for header then Drupal generate a TH tag for you, and use FALSE as if not. But the surprise thing is the result is two THs instead of TH & TD.

Col1Col2
Col1Col2

Well, it's a not a serious bug. It just gives you undesired result. If you take a look at Drupal implementation you will see that Drupal use isset to check for header flag, but isset actually check for array key existence and as long as header field is exist, Drupal will always render a TH for you.

Add new comment