Creating a "smart" search field for views

Events happening in the community are now at Drupal community events on www.drupal.org.
nvahalik's picture

I've got a site running some old but concise code that essentially allows people to search 3 different fields using a single field. A user can type in a zip code, county, or a name. If the script detects a 5-digit number, it geocodes the zip code using a built-in database and then performs a geocode search. If they type a regular string, it goes against both the county and the name fields.

I've seen a couple of articles that utilize hook_views_pre_view() to look at the arguments and modify the view based on the arguments that are passed. Is that the best way, essentially it might look like this:

  • Create a set of rules. Each rule would have a regexp or test, and if it passes that test, it uses the associated rule.
  • When executing a view, implement this logic:
  1. Load all of the rules.
  2. Loop through all of the rules until it finds one that matches (the last one is the default)
  3. Modify the view to look at that rule's columns and set the appropriate arguments.

Is there something that does this already? Is there a better way to do this?

Comments

Search issue

govindtotla's picture

Hey!! i am also looking for the same solution. have to apply a search for all fields like username, node title, node body etc. with only one exposed field. which is a mannered solution for this.
Thanks

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: