Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding sorting to a view on Drupal?

I used to know how to do this, but I can't seem to get sorting to work on a view where filters are exposed in a block. I want to be able to filter by, for example, type, price etc, but then also have sorting options to sort by these items.

How do I get sorting to work like this?

like image 993
coderama Avatar asked Feb 28 '23 21:02

coderama


2 Answers

I used that code to override sorting in non-table views

function views_tweak_views_query_alter(&$view, &$query) {
  if ($view->name == 'products'){
  if (arg(3) == 'pu') $query->orderby[0]='uc_products_sell_price ASC';
  if (arg(3) == 'pd') $query->orderby[0]='uc_products_sell_price DESC';
  if (arg(3) == 'nu') $query->orderby[0]='node_title ASC';
  if (arg(3) == 'nd') $query->orderby[0]='node_title DESC';

  } 
}

and placing into view template links with those urls

like image 110
451F Avatar answered Mar 12 '23 00:03

451F


AFAIK you can't expose sort criteria like you can with filters.

I looked a bit around a found this module. The idea is to create several views each with a different sort criteria and link them together with tabs. It's a bit hackish and might not work with exposed filters. The module is still in beta release, and I haven't tested it, so can't say if it's any good.

like image 37
googletorp Avatar answered Mar 12 '23 00:03

googletorp