2

I have created a view with four exposed filters. In exposed form settings, I added a reset link and used auto submit option. Filter and reset works fine without error, but I want to show the reset button only when the filter is applied. If no filter is applied, there should not be reset button.

I have searched, but I didn't find any solution.

How can I achieve this?

3 Answers 3

1

hook_form_alter() can also do this

function module_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'Your_form_id") {
    if (!$form_state['view']->exposed_input) {
      unset($form['reset']);
    }
  }
}

You will have some other value for ['view']->exposed_input. Use dpm(0 to get the exposed_input value. ['view']->exposed_input returns nothing if filter is not applied. Hope it works.

0

You can achieve this either using custom module[hook] or jQuery.

Using jQuery

  1. Hide Reset button using CSS display:none.
  2. Now in jQuery, on page load, check for URL containing Query String.

Example: [view-URL]/?type=page.

  1. If URL contains any Query String, then make Reset button visible.
2
  • How can do it using form alter @Nikhil Banait
    – user50991
    Oct 23, 2015 at 6:22
  • Sorry, I am not the right person to explain you Custom thing :( Oct 23, 2015 at 6:25
0

Thanks, Vamsi! I a bit fix your code.

/**
 * Implements hook_form_FORM_ID_alter().
 */
function MY_MODULE_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  // Show reset button in a view only when filter is applied.
  if (isset($form['reset']) && empty($form_state['view']->exposed_input)) {
    unset($form['reset']);
  }
}

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.