Drupal 7 come bundled with simple search module. It has almost no configuration and many limitations. But many of them could be solved with custom view. Things like searching in certain content types, languages or ajax pagination is one click away! Moreover I show you how to create own views area plugin.
In today's tutorial we build something similar to:
Which modules we need?
- Search - of course, this module is mandatory, because it builds our search index with CRON
- Views - probably already installed on your site
- Custom module - just few lines of custom code (will explain later)
How to configure custom view?
Follow highlighted lines in screenshot provided below or simply download export and modify as you wish.
- Sort criteria - Search: Score (desc)
- Path - search/node/% - this overwrites default search page
- Access - Permission | Use search - leverage search permissions
- Header - Global: Search block (provided in custom module)
- Contextual filters - Search: Search Terms (select display contents of "No results found")
- No results behavior - Global: Unfiltered text
- Use AJAX - Yes
Why we need custom module?
On default search page there is search form just before results. It cannot be achieved with any contrib module (views block area will not show searched text in textfield). However it's not hard to create own views area plugin and get it done. I suppose that you already created your first Drupal 7 module and you know where to put it. If not please visit examples for developers.
MODULE.info - do not forget to include our handler
MODULE.module - specify that our module uses views
views / MODULE.views.inc - define our area handler
views / handlers / MODULE_handler_area_search_block.inc - our handler
Now just enable your newly created module and clear cache.
Results displayed as view is very powerful
Drupal 7 search module is really simple with almost no configuration. When you want to create something serious, it's better to use Search API. But on many small sites there is no budget to create custom search and basic search module is only way to go. Let's deal with few weak spots with views.
Search only in certain content types
How many times you want to deny search in certain content types? Almost every site uses slides (as separate nodes), webforms or another contents where there is no point to have them searchable.
Just create new filter: Content: Type and it's done!
Search only in current site language
Create new filter: Content: Language and select Current user's language