Click Fraud Protection
PHP related blog, freelancer to hire!

How to modify Drupal Views SQL

I wanted to sort my views result on field which doesn’t exists in db table, so my idea was to use:

SELECT … WHERE field IN (value1, value2) … ORDER BY field = value1 DESC, field = value2 DESC  etc

Which orders my elements exactly as I need, but problem was how to modyfy views sorting options?

Simply you can implement hook_views_query_alter(&$view, &$query) in your_module.views.inc file

$query->orderby is an array of all orderby elements, so you can easily modify it.

Leave a Reply