0

Is there a way we can specify which link to use to sort a drupal table, or atleast, specify some parameters to the header links?

To be more clear, my table uses the following link when I click on the headers to sort the table: http://mysite.com/node?sort=asc&order=Name

What I would like the header link to look like is this: http://mysite.com/node?nid=123&pager_element_id=3&sort=desc&order=Name

Is there any way we can give the parameters to the header links?

1 Answer 1

0

The code that outputs the links for the table header is contained in tablesort_header().

$cell['data'] = l($cell['data'] . $image, $_GET['q'], array('attributes' => array('title' => $title), 'query' => array_merge($ts['query'], array('sort' => $ts['sort'], 'order' => $cell['data'])), 'html' => TRUE));

The function is called from theme_table(), which is the theme function used to render a table.

What you can do is implementing your theme function that is then used to render the table your module is outputting. There isn't any other method, as theme_table() is not getting any parameter that changes the URL for the table header; as such, implementing hook_preprocess_table() would not achieve anything.

To implement a theme function, you need to declare it using hook_theme().

function mymodule_theme($existing, $type, $theme, $path) {
  return array(
    'mymodule_table' => array(
      'variables' => array('header' => NULL, 'rows' => NULL, 'attributes' => array(), 'caption' => NULL, 'colgroups' => array(), 'sticky' => TRUE, 'empty' => ''),
    ),
  ); 
}

Once you declared the theme function, you can use it as you used theme_table(), simply replacing any call to theme('table', /* the rest of the arguments */) with theme('mymodule_table', /* the rest of the arguments */). Notice that in hook_theme() the theme function is referred as mymodule_table, but the PHP function is theme_mymodule_table().

The code you need for your theme function is the same used by theme_table(), except that instead of calling tablesort_header() it will call your own function.

foreach ($header as $cell) {
  $cell = tablesort_header($cell, $header, $ts); // Replace it with the call to your function.
  $output .= _theme_table_cell($cell, TRUE);
}
1
  • Could you please clarify, as I don't grasp the idea of implementing it in the theme function.
    – SN_26
    Commented Oct 3, 2012 at 5:48

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.