3

I've read a few posts on this and there does not seem to be an answer to that, is that right?

My exposed filters allow the user to change the sorting, but the url is kinda bad:

?sort_by=field_rating instead of just ?sort_by=rating, for instance. Can this be achieved somehow? Is it advisable to try out better exposed filters module when you have already kind of customized your exposed filters? /:

6

For order_by filter you can just set the filter identifier:

enter image description here

Although there is no option to set the identifier for sort_by filter, you can achieve your goal by altering the form.

Example

/**
 * Implements hook_form_alter().
 */
function YOURMODULE_form_alter(&$form, &$form_state, $form_id) {

  if ($form['#id'] == 'YOUR-FORM-ID') {

    // fields which you want to rename
    $form_state['my_custom_names'] = array(
      'field_rating_value' => 'rating'
    );

    // rename option values
    foreach ($form_state['my_custom_names'] as $name => $new_name) {

      $form['sort_by']['#options'][$new_name] = $form['sort_by']['#options'][$name];
      unset($form['sort_by']['#options'][$name]);  

      if ($form_state['input']['sort_by'] == $name) {
        $form_state['input']['sort_by'] = $new_name;
      }
    }

    // the custom submit handler need to be fired before the original one
    array_unshift($form['#submit'], 'my_custom_exposed_form_submit');  
  }
}

/**
 * An additional submit handler for exposed filters.
 */
function my_custom_exposed_form_submit(&$form, &$form_state) {

  // set the form state values to their original names.
  foreach ($form_state['my_custom_names'] as $name => $new_name) {
    if ($form_state['values']['sort_by'] == $new_name) {
      $form_state['values']['sort_by'] = $name;
    }
  }
}

Make sure to replace YOURMODULE and YOUR-FORM-ID with proper names.

2
  • that doesnt work for sorting nor the way tids show up in the url
    – Alex
    Sep 24 '13 at 7:46
  • because there are no filter identifiers in sort fields?
    – Alex
    Sep 24 '13 at 12:37
0

That's how views are built, the only way to fix that would be to change how you filter, you can try using views contextual filters and see if that works for you. But exposed views filters equate to query strings with field names, otherwise how would views know that "rating" actually means "field_rating" without adding additional overhead to the database.

1
  • i dont know, i thought there might be some alias-ing method. better exposed filters didnt work either. i am afraid i gotta do this with htaccess regex
    – Alex
    Sep 24 '13 at 7:48

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.