Quantcast
Channel: Archdiocese of Saint Louis's blog
Viewing all articles
Browse latest Browse all 3

Drupal Views Filters: Making Exposed Searches User-Friendly

0
0

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).

Parish Search by Name

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():

<?php
/**
* Implementation of hook_form_alter().
*/
function custom_form_alter(&$form, $form_state, $form_id) {
  if (
$form_id == 'views_exposed_form') {
   
// Add custom validation form for parish search
   
if ($form['#parameters']['1']['view']->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:

<?php
/**
* Makes parish search easier.
*/
function custom_parish_form_switcheroo($form, &$form_state) {
 
// Convert abbreviations (like "St.", "Sts.", "St", "St")
  // Also, sometimes there is a "/" right before the "St." part, so strip that
 
$form_state['values']['name'] = preg_replace('/^(\/)?st(s)?(\.)? /i', '$1Saint$2 ', $form_state['values']['name']);
 
// Change ampersand to "and" ("Joachim & Ann" becomes "Joachim and Ann")
 
$form_state['values']['name'] = preg_replace('/&/i', 'and', $form_state['values']['name']);
 
// Strip "'s" (eg, "St. Sabina's" turns into "St. Sabina")
 
$form_state['values']['name'] = preg_replace("/'s$/i", '', $form_state['values']['name']);
}
?>

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!

Average: 3.1(16 votes)

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images