2

I have a panel which displays a page that was created in views. When a user clicks the next button on the pager, it displays the error "An illegal choice has been detected. Please contact the site administrator."

I've tracked this problem down to being caused by my exposed fields I use for sorting: When the pager is clicked, it submits to a URL like http://mysite.com/reviews?myfieldtosort_tid=All&sort_by=&sort_order=DESC&page=0%2C0%2C1.

The problem is that sort_by is empty; if I remove that attribute entirely the problem goes away. Alternatively, if I use the exposed sort, it adds the sort field and also works correctly.

What can I do to still use the pager, but have it remove the sort_by field when it is not needed?

3 Answers 3

2

muka's answer is almost working, but if you have more than one filter, it overrides the additional filters. See the revised code:

function YOUR_THEME_form_views_exposed_form_alter(&$form, &$form_state) {
    if($form_state['view']->name == 'VIEW_NAME') {
        $sort_by = 'SORT_BY'; // Set Default Sort by Manually (see url for correct name)
        if(isset($_GET['sort_by'])){ // In case you have more filters you don't want to override them.
            $sort_by = $_GET['sort_by']; // Additional checks might be a good idea
        }
        $form_state['view']->set_exposed_input($_GET);  
        $form_state['input']['sort_by'] = $sort_by;
    }
}
1

I've solved by setting a default value in the form alter callback for views exposed form

function mymod_form_views_exposed_form_alter(&$form, &$form_state) {
  if($form_state['view']->name == 'yourviewname') {
    $_GET['sort_by'] = 'default_sort_name';
    $form_state['view']->set_exposed_input($_GET);  
    $form_state['input']['sort_by'] = $_GET['sort_by'];
  }
}
0

Slightly modified version of muka's code for multiple filters:

function yourmodule_form_views_exposed_form_alter(&$form, &$form_state) {
// if($form_state['view']->name == 'yourviewname') {
    if($_GET['sort_by'] ==''){
    $_GET['sort_by'] = 'your_def_sort';
    $form_state['view']->set_exposed_input($_GET);
    $form_state['input']['sort_by'] = $_GET['sort_by'];
   }
// }
}

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.