2

I am using an exposed Views Selective Filter which limits the options available quite nicely. However the display order of the Options is what is causing me the trouble. Here is an example of my Select list:

Accomodation - Camp Sites - Hotels - Rural Hotels

Beauty
- Hair and Nails - Makeup - Spa

In the results these display exactly as I want them which is grouped by the Parent term (Accomodation, Beauty etc...). The trouble is that when I use the Views Selective Filter to only show options with results the list of options shows like this:

  • Camp Sites
  • Hotels
  • Rural Hotel
  • Hair and Nails
  • Makeup
  • Spa

What I really want to do is to resort that list alphabetically before it is displayed to the User. I guess Hook Form Alter is the way to go but I can find anything anywhere on how to create the alpha sort option. I hope that all makes sense. Thanks

2 Answers 2

3

j.Rowny's answer doesn't work because $form_state is not passed by reference. You must actually modify the $form variable. This worked for me:

function yourcustommodule_form_views_exposed_form_alter(&$form, $form_state) {
    foreach($form_state['view']->filter as $filter) {
        if($fid = $filter->options['expose']['identifier']) asort($form[$fid]['#options']);
    }
}
1
  • 1
    Just a comment on $fid, abbreviations can make code confusing, I thought "where is he getting file ID from?"
    – Duncanmoo
    Commented Nov 29, 2013 at 9:19
0

I think something like this might work:

function yourcustommodule_form_views_exposed_form_alter(&$form, $form_state) {
   usort($form_state['view']->filter, "yourcustommodule_cmp");
}

function yourcustommodule_cmp($a, $b)
{
    return strcmp($a["whatever_property_you_are_sorting"], $b["whatever_property_you_are_sorting"]);
}

Basically you're using PHP's usort on the filter form's view filters. I have never tested this and have no idea if it actually works. Also, you'd need to find what property of the filter you'd actually want to sort as I don't know it off the top of my head. Alternatively, in non-critical UI situations, I don't consider it a terrible "no no" to just use jQuery. It'd take a few lines of jQuery to sort your options.

See usort documention for more info on using usort.

3
  • Me neither. Do you have any info on how to do it? Thanks for your help
    – Adrian
    Commented Feb 22, 2012 at 19:14
  • Did you try it? Commented Feb 22, 2012 at 20:11
  • I havent Im afraid as I tried to read through the cmp info and also some background on how to do it with jquery and most of it is over my head. I love Drupal but I have a long way to go still with php and jQuery. Id be happy to pay somebody to do this though. Thanks again
    – Adrian
    Commented Feb 23, 2012 at 11:36

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.