New Drupal Module: Views Search

A feature request for one of my recent projects was an "Advanced Search" functionality, similar to the ones that is commonly found in social networking and personal sites. Rather than creating a completely custom search form using Drupal's Forms API, I decided to create a module called "Views Search" which uses the Views module and allows anyone to create advanced searches for anything Views can process without custom coding.

Views' exposed filter work very similar to the search functionality, however exposed filters in the Views module work like true filters, where a list of nodes is limited down to the intended results using a combination of several filters. In contrast to this approach, a more traditional search form allows users to define all criteria first (before seeing any results) and then click on a "Search" button to see the results. This module allows site administrators to easily use the behavior of a traditional search for selected views.

In addition, the Views Search module allows Drupal site members to save their searches for future usage.

Here's an overview of the feature set:

  • Separates the search form and results: selected views show a search form without showing the results on the first page and the search results without showing the search form/filters on the following pages
  • Allows users to start over with a search (using a button on search results page)
  • Allows users to modify the search criteria (using a button on the search results page that expands search criteria using jQuery)
  • Allows users to save a set of search criteria for later usage
  • Allows users to modify/overwrite the name and search criteria for a saved search
  • Provides a default view that allows the current user to manage saved searches
  • Provides a block showing a drop-down with saved searches for the current user
  • Enables site administrators to select views that should act like "search views"
  • Enables site administrators to limit the number of saved searches for certain user roles & set saved search limits per rule
  • Enables site administrators to batch-delete all saved searches for a specific view

You can download the module from the Views Search Module page on Drupal.org. I'm looking forward to any feedback.

Comparison of Views Exposed Filter and Views Search

Comments

Hi

I really like the views modules because I need to implement something similar on a website I am making for a new firm. It is going to about travelers who might want to rent hostels.

So I want them to be able to search according to their preferences. What I dont understand is how to implement a search block which could search within a range e.g. the users selects a min. and max price and gets a results according to it. I see here that in the screenshot you have allowed for search according to min and max age. Can you help me with this?

Thanks!

Hi there. Sure, I'd love to help you out. If you want users to search within a range (in my case age range), you would add two exposed filters on the same field in views. Basically, you would do this in the "Filters" section of Views:

  • First, you add the "larger than" filter for your numeric field (in your case price). Expose that field and set the Operator dropdown to "Is greater than or equal to"
  • Then, you add the "smaller than" filter for the same numeric field (in your case price). Expose that field again and set the Operator dropdown to "Is less than or equal to"

With those two exposed filters in place, you and your site users should be able to search for results within that range.

Hope this helps.

Sorry for replying terribly late.

Thanks for your help. However, when I do as you said above it simply displays a text field rather than a drop down list, which is because the price field in that content is simply a text box (added using Money CCK) and not a select list. Is there any way I can rewrite the view filter as a select list like you have on real state websites.

Really appreciate your help!

Faizan: You can change the HTML widget that Views uses, but you need to do some custom coding. I'm afraid I can't give you an exact example, but you basically you need to do the following things:

1. Create a helper module and activate in your Drupal instance
2. Use Drupal's Forms API and implement hook_form_alter to change the widget type.
3. To find out the name for the form field you need to change, go to the exposed filter in your view and look for the "Filter identifier". In your module, set the widget type of that field to a select filter and define the options for the field.

In case you're not familiar with the Form API, here's the Quickstart Guide.

Happy coding!

Hi,
I really like this module but i want to ask something whether this module is similar to that of search module and does this module can be implemented without views module?

Hi there. In terms of the functionality, this module is similar to the search module (it returns search results), but it depends on the Views module and cannot be without it.

Hi,

I stumbled here from Views Search project page. I'm in the process of building a Drupal-based social network and I was just looking around for some way to make searching people (a View) a bit more user-friendly, when I found your module.

I like very much how you arranged the search form in the screenshots you posted; since I'm going to build something exactly like that, I'd like to know what site they come from, so I can take a look at it "live" (and get some inspiration from your excellent work :D).

Thank you very much for your help (and the very useful module!).

Hey there M. Why don't you send me an email (http://www.danielhanold.com/contact) and I'll send you the link to the site so you can look at the example live. To arrange the search form similar to the form in the screenshot, there's a bit of additional programming required, but if you're familiar with hook_form_alter, that shouldn't be a problem. Looking forward to your message. Danny

Hi Daniel,

Thanks for your help

You're very welcome.

Hello Danny

Please i would like you to help me out.i want to setup a website with search fir cck fields example search for lawyers and search for law firms,under search for lawyers u will have search options like first name,last name country and then u click on search buttom and then the search result will give u what u chose for ur fields in views.Please i will be very very happy.Thanks very much and hope to hear from you soon as possible.

Hi Salisu: There's already a couple of good pages / videos out there about this. Here are a few links:
http://yadadrop.com/drupal-video/drupal-views-exposed-filter-1
http://drupalsn.com/learn-drupal/drupal-tutorials/creating-community-sea...