Is Drupal Tablesort broken in design?
Every Drupal developer knows about tablesort API. It's useful when you work with table list, especially is the tablesort_get_order and tablesort_get_sort functions. But I think these functions are broken in design. Here is how it is broken.
It's a good habit to separate the presentation and the business logic. In Drupal, we do this by introduce theme functions. Theme functions are only used to generate the presentation, or HTML code here and should not do any logic processing. When you work with a list, you will usually construct a query, fetch it to array and pass it to theme function to render the page. Sometime you have to travel the query result to do some custom business logic, to calculate a new value, for example. And I consider ordering the data is a part of business logic. But if you use tablesort API, you will likely to mix your business code with your presentation code, since table sort functions require you to have the $headers, which describes table headers, to get current sort field and sort order. Take it like you want to sort your data before passing it to theme function, but tablesort API requires some information which is not available in business function so everything is mixed up.
Through, there are some advantages for requiring $headers, such as you can hide your table schema from user. We can see a related issue is raised here, but I don't think any changes will be made. So we gonna keep $headers variable in our business code for a while.
Post new comment