Other recent blogs
For drupal development, it is required to display content using Views. Drupal allows us to create content with content type on our website. Content created in these content types can be displayed in various formats on drupal website with the help of views module. Sometimes, it is required to allow users to filter the content according to the fields available in content type and this functionality is provided by view exposed filters. In this blog, we will see what happens when end user is allowed to search multiple fields with single search field in view display page.
Let's create 'Event' content type which stores details of upcoming drupal events with field titles, details, start date, end date, contact person and email. Now, we need to show all events created in the site. For this, we create a simple view which shows list of content posted in event content type. Make sure that you choose fields in display format while creating the view. Below are basic settings of the view which you need to choose at the time of creating this view.
This will provide us with a simple list of all events posted under event content type. Now, we will add search filter which allows user to search content in this view according to multiple fields with single search box.
For this, add another filter called 'Global: Combine fields filter'.
In filter configuration form follow the instructions given below:
1. Check "expose this filter to visitor" checkbox and in 'operator' field.
2. Select 'contains' to search text.
3. In 'Choose fields to combine for filtering' field, select fields which you want your users to base their search on. Right now we want the user to search for event according to title and body.
After applying this exposed filter, we will get a search box on top of the view display page and with this search box we can search content listed in the view.
One of the best uses of global combine fields filter is when we want to search a particular type of content from a search block on one of the regions on the site. For this, in views advanced area there is an option called "Exposed form in block:" Set this option to "yes" and save the view. Now go to blocks listing, there will be a new exposed form block with same name as your view. Assign this block a region so that views exposed search box will show up in selected region.
Now, you will see that your search filter is shifted from views top region to your selected region. This search block is available on whole site, so it can work as global search for only events in the site. If we search 'London' in search box from any page of the site, it will take us to the upcoming Drupal events view page with search results of events content which contain 'London' text in its title field or body field.
As you can see in above screen shot that for better usability, I changed block name to 'Search Upcoming Events' from block configuration and 'Filter' button to 'Search' button in views exposed form style settings. With global combine fields filter, we can add more exposed fields filter criteria for detailed search. It also allows us to create different search blocks for different type of content.