Drupal Views Filters: Making Exposed Searches User-Friendly
One of the main new features of the Archdiocese of Saint Louis' website (to launch on February 22!) is the much-improved parish and school searching capabilities. There are many facets to these sections of the site; everything is built using the combination of nodes built with CCK, Views, and Mapstraction (for Google Map interfaces).
One of the main annoyances with most implementations of parish and school searching that I've found (and I've tested almost every U.S. Archdiocese's website for this functionality) is the fact that searches are extremely rigid - if you don't type in the exact terms for the title of the parish in the parish database, you won't get any results.
For instance, type in "St. Luke," and you might get a result for St. Luke parish. However, type in "Saint Luke," and you get nothing. Or, what if you type in "Saints Joachim and Anne," but the parish is in the database as "Sts. Joachim & Anne"?
To message search data, we used a few regex filters on the user input, and Drupal allowed us to do this pretty easily by hooking into the exposed form on the parish and school search pages.
Hooking into Views' Exposed Forms
First, you'll have to create a custom module for your Drupal site. Inside that custom module, you'll use a hook_form_alter() to alter the user input of the views exposed form just before it's submitted to the search. Here's our form_alter():
name == 'parish_directory') { $form['#validate'][] = 'custom_parish_form_switcheroo'; } } } ?>
We are using a "parish_directory" view, so we tell hook_form_alter to only apply our custom function, 'custom_parish_form_switcheroo,' to the parish name search form.
And here's our custom function, which takes the form data as entered by the user, applies some regular expression replacements (using PHP's preg_replace()), and then sends that data to the search filter:
Using these rules, we are able to allow people to search for "St., "Saint," "Sts.," "Saints," "St," etc... they can also type in "Saint Sabina's," and get the proper result. basically, we're allowing users to not have to worry as much about data purity.
In an ideal world, Views would provide a built-in 'did you mean' filter, but until then, we'll have to improve user experience in Views searching using baby steps...
I would like to thank Joel Stein for his excellent help in setting up this feature on the Archdiocesan website!