0

I have develop a drupal view with table format.

  • Here table rows are Name and Date.
  • I am sorting a Name column with default sorting in views,it's working fine.
  • But when user click on views page automatically views page redirected to default sorting in views.
  • Here problem is when user click on sorting with (asc/desc) that order permanently store in database.

how can we achieve this.

5
  • Are you using views content or query caching?
    – thumbson
    Commented Feb 12, 2015 at 6:01
  • i am using views content
    – RaMeSh
    Commented Feb 12, 2015 at 6:35
  • can you provide a screen shot of your view setup?
    – No Sssweat
    Commented Feb 12, 2015 at 7:44
  • I think you just have to click the downward arrow next to the add button of the sort field and re-arrange your sorts so that the sort by name is listed as #1.
    – No Sssweat
    Commented Feb 12, 2015 at 7:51
  • please see this question for screen shot: drupal.stackexchange.com/questions/147071/…
    – RaMeSh
    Commented Feb 12, 2015 at 10:47

1 Answer 1

0

You could alter your view query with hook_views_query_alter to add your custom sorting.

<?php
/**
 * Implementation of hook_views_query_alter
 * @param type $view
 * @param type $query 
 */
function mymodule_views_query_alter(&$view, &$query) {

  //simple example: change the order of the master display
  //if you want to do it only  on a certain display add something 
  // like  "&&$view->current_display == 'panel_page_1'"
  if ($view->name == 'VIEWNAME') {

    //to find out what the fieldname ist: use devel and add your desired field as 
    //first filter ( =>orderby[0]). install devel and uncomment the next line
    //dpm($view->query->orderby)

    $view->query->orderby[0]['field'] = "CASE FIELD_NAME WHEN 'apple' THEN 1 WHEN 'zebra' THEN 2 WHEN 'banna' THEN 3 ELSE 4 END";
  }
}
?>
1
  • can you elaborate this one plz .$view->query->orderby[0]['field']
    – RaMeSh
    Commented Feb 11, 2015 at 12:52

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.