Drupal developer

Display search results as view in Drupal 7

Posted on 30. November 2015 - 10:47 in category Drupal 7

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.

  1. Sort criteria - Search: Score (desc)
  2. Path - search/node/% - this overwrites default search page
  3. Access - Permission | Use search - leverage search permissions
  4. Header - Global: Search block (provided in custom module)
  5. Contextual filters - Search: Search Terms (select display contents of "No results found")
  6. No results behavior - Global: Unfiltered text
  7. 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

<?php
name
= MODULE
description
=
core = 7.x
package
= Custom

; Views handlers
files
[] = views/handlers/MODULE_handler_area_search_block.inc
?>

MODULE.module - specify that our module uses views

<?php
/**
 * Implements hook_views_api().
 */
function MODULE_views_api() {
  return array(
   
'api' => 3,
   
'path' => drupal_get_path('module', 'MODULE') . '/views',
  );
}
?>

views / MODULE.views.inc - define our area handler

<?php
/**
 * Implements hook_views_data()
 */
function MODULE_views_data() {

 
$data = array();

 
$data['views']['block-search'] = array(
   
'title' => t('Search block'),
   
'help' => t('Insert a search block inside an area.'),
   
'area' => array(
     
'handler' => 'MODULE_handler_area_search_block',
    ),
  );
 return
$data;
}
?>

views / handlers / MODULE_handler_area_search_block.inc - our handler

<?php
/**
 * @file
 * Block area handlers. Insert a block inside of an area.
 */
class MODULE_handler_area_search_block extends views_handler_area {

 
/**
   * Render the area
   */
 
function render($empty = FALSE) {

   
$search_block = drupal_get_form('search_form', NULL, check_plain(arg(2)));
    return
render($search_block);

  }

}
?>

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

Comments