Drupal 7 Views (3) provides great functionality. However the optionally provided "Reset" button for exposed filters does not work nice with AJAX.
If it is clicked it:
- Does not fire via AJAX but loads a whole new page
- Has a problem with the URL (see https://www.drupal.org/node/1109980) and so redirects the user to the front page in most cases.
That's not nice and we need a solution that works. Furthermore we don't want the better_exposed_filters.module, but a lightweight solution.
So we developed a simple replacement, which detects if the reset function is activated in the views settings ("Exposed Form" > "Exposed Form Style" > "Settings) and replace it with a jquery.forms.js: formReset()-function and a simple trigger for the AJAX functionality.
So here's the code:
/**
* Implements hook_form_alter().
*/
function MYMODULE_form_views_exposed_form_alter(&$form, &$form_state) {
// ------------------------------------------------------------
// Add an own reset button to all views exposed forms. The reason behind is,
// that the default Drupal reset button doesn't play nice with AJAX
// and jumps back to the front page because of URL problems. So it's unusable for us.
//
// Add class to make it better selectable in JS and CSS
$form['submit']['#attributes'] = array('class' => array('my-views-filter-submit'));
$submit_btn_id = $form['submit']['#id'];
if (!empty($submit_btn_id) && !empty($form['reset'])) {
// Add own reset button
$form['submit']['#suffix'] = '';
// Remove the original RESET button.
unset($form['reset']);
}
}
?>
I hope it works well for you and please leave a comment, if you liked it :)