1

I created a views page using foo plugin.When a user visit the page the city and Clinic/Hospital columns by default are being rendered alphabetically (ASC) as in below screenshot.

EDIT: Let's say for example. City is Bangalore and only for Bangalore Clinic/Hospital(C/H) column is alphabetically ordered(say Hospitals A,B,C,D...).Then user clicks on City head,then for Bangalore C/H column is alphabetically disordered(ex B,D,C,H...) but records are still correct to Bangalore city means those clinic/hospitals are located in Bangalore .Same goes for other cities too.What I want is C/H col should be ordered as earlier (alphabetical) City by City but no matter how city is ordered.

enter image description here

When user clicks on City head (which is sortable),the order of Clinic/Hospital goes heywire (no more alphabetical order) like below image:

enter image description here

Problem: How can I make my Clinic/Hospital order intact even after user clicks on City head.

Below is my views configuration: enter image description here

I am using Drupal 7.41 and views 3.0

Tried so far:

function mymodule_views_query_alter(&$view, &$query) {
   if($view->name == 'list_of_doctors' && $view->current_display == 'page_1' && count($query->orderby) == 1)
    {

        $query->orderby[1] = array(
             'field' => 'field_data_field_clinic_hospital_field_clinic_hospital_value',
             'direction' => 'ASC',
            );
    }
 }

}

But after clicking City head,whole table vanishes.

  • This doesn't make sense; if you want to sort by city and keep the order of clinic/hospital intact, then you will not have the same information in rows anymore. How do you want to show the information in 1 row if there are 2 sort filters? – Aram Boyajyan Feb 25 '16 at 12:58
  • Please see my edit – Hiranya Sarma Feb 25 '16 at 13:42
0

As I understand you want to be able to use a secondary sort after overriding the default sort by clicking on a column header.

This can be achieved from the front-end using jQuery plugin TableSorter as described here: https://drupal.stackexchange.com/a/125188/22915

Note this won't work for views with pagination.

It can also be achieved with some custom PHP code. Add the following to a custom module and change the CAPITALIZED words accordingly.

/**
 * Implements hook_views_query_alter().
 */
function YOUR_MODULE_NAME_views_query_alter(&$view, &$query) {
  if ($view->name === 'YOUR_VIEWS_NAME' && $view->current_display === 'YOUR_VIEWS_DISPLAY_NAME' && count($query->orderby) == 1) {
    $query->orderby[1] = array(
      'field' => 'YOUR_SECONDARY_SORT_FIELD',
      'direction' => 'ASC',
    );
  }
}

This code snippet will make sure that, no matter what column you use as main sorting, you always use a certain field as secondary sort. In your case this would be the Clinic/Hospital field (use system name in code).

Note: if the secondary sort field is not a sortable column, you can't use the query alias. So YOUR_SECONDARY_SORT_FIELD needs to be something like: 'field_data_field_name.field_name_value' instead of 'field_data_field_name_field_name_value'.

  • See my code above. – Hiranya Sarma Feb 26 '16 at 7:12
  • You should put your field name (field_data_field_clinic_hospital_field_clinic_hospital_value) in quotes. – dmsmidt Feb 26 '16 at 9:53
  • Code corrected but it stopped sorting for City completely – Hiranya Sarma Feb 26 '16 at 10:32
  • Can you share the Views format 'footable' settings? And could you try with just a the 'normal' table formatter? And can you dump what is in $query->orderby[0] when you click on the 'city' in the table header? – dmsmidt Feb 26 '16 at 10:37
  • Pls refer: drive.google.com/… – Hiranya Sarma Feb 26 '16 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.