Adding a Reset Button to Exposed Views Filters
- Tags:
I'm a big fan of using exposed filters in my administrative views that I build. One thing that had escaped me until today was that there isn't always a really easy way for my users to get back to square one. I need a reset button. Don't we all?
I did some searching around and some experimenting. It turns out that you can do this really easily in a custom module just using a hook_form_alter(). You can see the result in the screenshot below. Easy reset button.
I have a module that I use for most of my sites that's just called "form_alterations". Not something worth contributing, but it's something worth having on hand for the sites that I build. That's the perfect spot for the following snippet of code:
<?php
/**
* Implementation of hook_form_alter().
*/
function CUSTOM_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'views_exposed_form') {
$exposed_views = array('NAME_OF_VIEW', 'NAME_OF_VIEW2');
if (in_array($form_state['view']->name, $exposed_views)) {
$current_display = $form_state['view']->current_display;
$form['reset'] = array(
'#type' => 'markup',
'#value' => '<input class="form-button" type="reset"
value="Reset" onClick="javascript:window.location=\'/'.
$form_state['view']->display[$current_display]->
display_options['path'] .'\';" />',
);
}
}
}
?>
The basic thing you need to enter into this snippet is just the names of your views. Using the $exposed_views array, you can list multiple views that need to have the reset button added.




Comments
hard-coded root path
thanks for this, mark--just what i needed.
i had a couple of the questions: 1) can't the drupal root slash could be left out without any issues? 2) doesn't an html form button with a value of 'submit' always clear its form (so that the path stuff, js function and page refresh are not necc. here)?
that would leave the $form[reset] array like this (which is working for me, but only tested a bit):
$form['reset'] = array(
'#type' => 'markup',
'#value' => ' value="Reset" />',
);
i am totally open to the possibility that i am missing something! thanks again for posting this.
Thanks to discussion above I
Thanks to discussion above I had it working in once, so perhaps one complete post can help others.
Using D6.11 + Views2
step 1
created folder: "form_alterations"
in "sites/all/modules/"
step 2
created file "form_alterations.info" in the folder from step 1
containing:
step 3
created file "form_alterations.module" in the folder from step 2
containing:
step 4
Replace
and
with your view names.
Btw. To get the view name: open your view to edit it, the last variable in your URL should be your view name.
step 5
Enable your module
step 6
Empty cache (use Devel of Admin_menu module)
Thanks for your great post Mark Jarrell!
Hope this is of any help to others.
Changing drop down to radio buttons
You seem like the fellow to ask...
I've gotten this far:
<?phpfunction custom_module_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'views_exposed_form') {
$exposed_views = array('micros_by_tag'); // this an array, so you can add more.
if (in_array($form_state['view']->name, $exposed_views)) {
$view = $form_state['view']; drupal_set_message('<pre>'.print_r($view->display['default']->handler->options['filters'],1).'</pre>');
/*** below is borrowed from V1 in D5. ***/
foreach($view->> *** NOT SURE *** as $count => $expose) {if (is_numeric($count)) {
if (!$form["filter$count"]['#options']) {
$form["filter$count"]['#type'] = 'checkboxes';
$form["filter$count"]['#process'] = array('custom_module_checkboxes' => array());
}
$form["filter$count"]['#required'] = TRUE;
}
}
}
?>}
}
Thoughts? Care at all?
Wait, I'm not sure what
Wait, I'm not sure what you're trying to accomplish. Can you explain a bit more? I'll take a look at your code if I know what we're trying to do.
how do you get it to only
how do you get it to only show the reset button if values are present in the filter?
Can't Get It to Work
I have two page views with exposed filters that I would like to add reset buttons to.
I created a my_form_module.info and a my_form_module.module. I copied and pasted your snippet into the module file. The only change I made to the snippet was to add my two views names as follows:
$exposed_views = array('today2', 'tomorrow');I enabled my module and refreshed my views page but, alas, no reset button....no errors...no change. I tried emptying cache but that made no difference.
What am I missing? Thanks!
ps I'm using Drupal 6.10 and Views 6.x-2.3
Mark, Any thoughts -
Mark,
Any thoughts - suggestions on where I am going wrong? Tks
Your Module Weight Might Need to Be Increased
Dear jdm,
For my "form_alterations" module that I use on my sites, I have a "heavy" weight applied to it. You might go to your "system" table and update your module's weight to something like "11". This will make sure that anything that occurs in your module occurs later than the Views module and most other modules. You can read more about what I mean here:
http://drupal.org/node/110238
If you want to, you can even implement this into an .install file inside of your module that will automatically set the weight on your module whenever it is enabled on your sites.
Mark, Sorry to be such a
Mark,
Sorry to be such a dunce on this...but I have tried everything I can think of and can not get the code to work. I added an .install file and set the weight to 11....then 20 and finally 100 but no go. I found that I had to uninstall the module each time to get the weight to change.
I tried adding a print () with a sentence to the bottom of my_form_module.module...refreshed the module and could see the sentence on my view page between the site "admin menu" at top and the page itself. Thus, it would seem that the module is configured properly.
Is there anything else I need to include in my module besides your snippet? Anything in template.php? Is there anything I need to change in my views settings? I am assuming "Reset" button should just show up next to the "Apply" button as you are showing in yours.
Thanks!!!
Adding Updates to Install File and Using dsm()
jdm,
One other tip I can give you on the install file (not sure if this will be much faster) is that you can also add update functions to your .install file.
function form_alterations_update_6001() {$ret = array();
$ret[] = update_sql("UPDATE {system} SET weight = 10 WHERE name = 'form_alterations'");
return $ret;
}
Then you have to hit update.php to run the update on the module/database. At least it keeps you from installing/uninstalling the module repeatedly.
My best guess is that something isn't setup exactly the way you think it is. You definitely need to enable the "Devel" module if you haven't already, and start using the dsm() function. This will print out variables, objects, and arrays for you in an easy-to understand and navigate way. I would start out by having your form_alter function run:
dsm($form_state['view']->name);Success!!
Ok, I finally figured it out. I am still getting my head around custom modules and forms.....so I didn't realize that where you indicated "CUSTOM_form_alter" that "CUSTOM" should be substituted with the name of my module. I guess that may be obvious to most, but you may want to consider adding that to your instructions......or not.
Mark, I appreciate you hanging in there with me on this. The other pointers you gave me will be beneficial as well.
I have bookmarked your site and will be a regular visitor.
Thanks again,
Jeff
Re: Success!!
Great! I'm glad you finally got it working on your site. :)
Added Current Display
I modified my code snippet above. I previously had it defaulted to "page_1". It should really be defaulted to whatever the current display is set to.
Error
Hello,
thank you for sharing this very much needed function.
Unfortunately, there is an error in the line starting with '#value'. Seems there is something missing in the code?
My guess
Try changing display['page_1'] to $display['page_1'] and see if that fixes it.
Michelle
Ya.
Alex is right, that line makes no sense at all... :(
It will be cool when it's fixed tho, I can use this right away! :)
Sorry
Sorry, I should have paid closer attention before publishing that one. My input filters were blocking out some of the code from appearing. It looks to be fixed now. Thanks!