rockstardev
rockstardev

Reputation: 13527

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?

Upvotes: 4

Views: 10798

Answers (5)

zhecsan
zhecsan

Reputation: 1051

Adding sorting to a view on Drupal less programm code in hooks.

You need to use arguments in display page.

Use taxonomy menu for pages before.. Next:

  1. Create one display page in view with path (for example): some_path/%/by_totalcount
    1.1. Make sort criteria for this display by totalcount
  2. Make another one dislplay page in this view with path (for example): some_path/%/by_date
    2.1. Make sort criteria for this display by date
  3. Create new block with code:

<?php $url= urldecode($_SERVER['REQUEST_URI']); switch($url) { case '1': $class = 'top'; $title_h2 = 'top'; break; case '/taxonomy/term/6 3 ': $class = 'travel'; $title_h2 = 'travel'; break; ................. } global $base_url; $url_rating = $base_url.'/'.arg(0).'/'.arg(1).'/'.arg(2).'/rating'; $url_created = $base_url.'/'.arg(0).'/'.arg(1).'/'.arg(2); ?> <div class="<?php print $class; ?>"> <div class="title"> <h2> <?php print $title_h2; ?></h2> <p>Sort node: <span class="sort_type"><a href="<?php echo $url_rating; ?>">by rate</a></span> | <span class="sort_type"> <a href="<?php echo $url_created; ?>">by date</a></span></p> </div> </div> <p>&nbsp;</p>

VOILA


Sorry for my english.. it isn't my native language ))


Upvotes: 0

Kelly Bell
Kelly Bell

Reputation: 41

Just in case you cannot find where to set this, look on the left side of the View (in Edit mode), under Basic Settings, select "Table". The click the "settings" (looks like a little gear icon to the right of the "table" selection), and you'll see a list of all the display fields, where you can select which ones are sortable/not, and which is the default sort.

Upvotes: 2

451F
451F

Reputation: 281

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

Upvotes: 4

Mike Crittenden
Mike Crittenden

Reputation: 5917

If you choose to use a table layout, you can sort by columns. That functionality is built into views.

Upvotes: 1

googletorp
googletorp

Reputation: 33265

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.

Upvotes: 2

Related Questions