2

My Views list of events is grouped by the date (using the Grouping Field checkbox in the Style settings).

But my exposed Sort Criteria can sort the list by either date or by name.

An alphabetical list of events grouped by date really doesn't make any sense.

What would make sense is for the Grouping Field to turn off when the Sort Criteria is by name.

I'm thinking either i do two views, and my template decides which view to embed depending on the URL query. Or perhaps a views hook like hook_views_query_alter() could turn off the Grouping Field depending on the circumstances?

1 Answer 1

2

Ok, i think i've managed it with a Views hook. Took an hour's worth of tinkering. I plopped this into my template.php...

/** Find out what the sort criterion is.
    @return The sort criterion fieldname. IMPORTANT: this isn't sanitised!
*/
function MYTHEME_get_sort_by() {
    if (isset($_GET['sort_by']))
        return $_GET['sort_by'];    
}

/** Turn off Views grouping-field when sorting by title.
*/
function MYTHEME_views_pre_build(&$view) {
    if ($view->name=='event_programme') {
        if (MYTHEME_get_sort_by()=='title')
            $view->display_handler->options['style_options']['grouping']='';    
    }
}
1
  • Note: i'm using Drupal 6.22, Views 3.0rc2 Nov 10, 2011 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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