I generate some data in one of my modules and generate a table using theme_table(). I want the fields in that table to be sortable, but the table is being rendered with the wrong value in the links.

I'm getting ?sort=asc&order=Birth Date instead of ?sort=asc&order=field_birthdate.

Here is the code I'm using to generate my table:

$headerColumns = array(
    array('data'=>'Name', 'field'=>'title'),
    array('data'=>'Birth Date', 'field'=>'field_birthdate'),
    array('data'=>'Sex', 'field'=>'field_sex'),
    array('data'=>'Stars', 'field'=>'field_stars')

return theme_table(array(
    'header' => $headerColumns,
    'rows' => $rows,
    'attributes' => array(),
    'caption' => '',
    'colgroups' => array(),
    'sticky' => true,
    'empty' => 'You ain\'t got none.',

I believe I'm generating that array according to the instructions on the theme_table() api docs, but it's not working.

How do I get the sort link url to properly use the fieldname I'm giving it in the render array?

EDIT: As requested, here is the code I use to generate my query:

$query = db_select('node', 'n')
            ->fields('n', array('nid'))
            ->condition('uid', $user->uid)

$direction = 'asc';
if (array_key_exists('sort', $_REQUEST) && $_REQUEST['sort'] == 'desc') {
    $direction = 'desc';

if (array_key_exists('order', $_REQUEST) ) {
    $fieldname = strtolower($_REQUEST['order']);

    $field_info = field_info_field('field_'.$fieldname);
    if ($field_info !== null) {
        $table_name = _field_sql_storage_tablename($field_info);
        $query->innerJoin($table_name, 'f', 'f.entity_id = n.nid');

        $query->orderBy(sprintf('f.field_%s_value', $fieldname), $direction);


$query->orderBy('title', 'asc');

if ($entityType !== null) {
    $query->condition('type', $entityType);

$records = $query->execute();
  • Not sure but maybe this: api.drupal.org/comment/30958#comment-30958
    – FriOne
    Aug 11, 2014 at 8:19
  • 1
    Can you show the query as well? Does it roughly match this?
    – Andy
    Aug 11, 2014 at 16:44
  • @FriOne when I use the format given on that page, then I don't get any sorting in the table at all. The headers just become simple text instead of links. Aug 11, 2014 at 18:33
  • @Andy I've added my query, but I'm not sure why that is relevant, because I'm not saying the query isn't sorting, I'm saying the rendering of the table is rendering the wrong field names in the table. The theme_table() method doesn't interact directly with the query. Aug 11, 2014 at 18:34
  • 1
    @Kenny field is: "The database field represented in the table column (required if user is to be able to sort on this column).". Since you don't have field_birthday etc in your query, you can't use them to sort. I think you're trying to partially use a feature that's meant to more complicated (dynamic table sorting). More info here
    – Clive
    Aug 11, 2014 at 19:08

1 Answer 1


You're doing too much work.

For sorting:

$query = db_select('node', 'n')
        ->fields('n', array('nid'))
        ->condition('uid', $user->uid);

$sort_query = $query->extend("TableSort")->orderByHeader($headerColumns);
$records = $sort_query->execute();

Using the TableSort extension will simplify things immensely.

  • Apparently! :) I was just realizing that as I was looking at the extend() function. The issue I'm running into now though is that the query fails because the field I'm sorting on it's part of node. Is there a way to get it to automatically join the field_data_field_birthdate table? Aug 11, 2014 at 20:21
  • try using the table alias in the $headerColumns array. For example f.field_name rather than field_name Aug 11, 2014 at 20:43
  • I can see in the error message that happens that the field_data_field_birthdate table hasn't been joined into the query. The table doesn't have an alias, because the table isn't in the query. In my original code, I do check for those additional fields and join the tables in. If I do that, I'm sure it'll work, but I'm trying to see if I'll still be doing "too much work" as Scott pointed out. I'm hoping it'll be able to join those tables in for me. Aug 11, 2014 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.