Views Exposed Filter Search Clean URL’s

Views offers the ability to expose filters to the end user so they may filter and sort through a views listing to find what they want in a large list of content. If you have used exposed filters before you will be familiar with exposing a filter on a specific field, such as the title field, for example.

Now for ex: say you have a content type having fields as follows

  • Categories
    • Academic program
    • Social awareness...
  • Type
    • Winner
    • Finalist

Firstly you’ll want to create a taxonomy vocabularies for categories and type fields of your content.

Then replace those field with taxonomy vocabularies.

Now create a view to display your desired content type. Here in this example “entries”.

In filters add search term field and make it exposed. This field is inbuilt and searches for taxonomy terms.

In order to achieve what we are doing you’ll want three contextual filters as shown below.

Image removed.

Now in order to obtain a clean URL you’ll have to get into backend.

Now coming to coding part of php - you’ll want to write a custom module.

  • Block info

* Implements hook_block_info().


function my_block_info() {

$blocks = array();

$blocks['search_filter'] = array(

'info' => t('Search filter'),


return $blocks;


  • Block Content

function my_block_view($delta='') {

$block = array();

switch($delta) {

case 'search_filter' :

$block['content'] = my_search_filter_view();



return $block;


The above code creates a block. Now inside block content am calling a function my_search_filter_view().

In the above function i will call a drupal core function to get a form.

function my_search_filter_view() {

$block = array();

$block['subject']= '';

$block['content'] = drupal_get_form(my_search_filter_form');

return $block;


Now is the main part to obtain clean URL’s when a search box is submitted

function my_search_filter_form($form,&$form_submit) {

$category_arg = 'all-categories';

$type_arg = 'all-applicants';

if(arg(2))$category_arg = arg(2);

if(arg(3))$type_arg = arg(3);

// in above Am setting my variable to the URL argument type

$categories = my_selectbox_contents(2);

$types = my_selectbox_contents(7);

//Now am going to define three fields and their types and also a submit button required for my search

$form['categories'] = array (

'#title' => '<h4>Categories</h4>',

'#type' => 'select',

'#options' => $categories,

'#default_value' => $category_arg,


$form['type'] = array (

'#title' => '<h4>Type</h4>',

'#type' => 'select',

'#options' => $types,

'#default_value' => $type_arg,


$form['keyword'] = array (

'#title' => '<h4>Search</h4>',

'#type' => 'textfield',


$form['submit'] = array(

'#value' => 'Submit',

'#type' => 'submit',


return $form;


/Using hook_form_submit am gonna define my URL path.

function my_search_filter_form_submit($form, &$form_state){

// Form above was submitted, now lets clean up $form_state['values'],

// that's the submitted values in an associative array.


// And here is the value that was submitted:


$type= str_replace( ' ', '-',$form_state['values']['type']);


$type= 'all-applicants';



$categories= str_replace(' ', '-',$form_state['values']['categories']);


$categories= 'all-categories';


$keyword ='';


$keyword =trim($form_state['values']['keyword']);


$search_path= 'gallery/2013/'.$categories.'/'.$type;


drupal_goto($search_path, array('query' => array('keyword'=>$keyword)));






// Till now you have defined how your URL will be clean. In short you have generated a clean URL.

Now let us populate the values into the dropdown/select boxes.

function dfe_selectbox_contents($vid) {

$terms = taxonomy_get_tree($vid);

if($vid== 2){

$output['all-categories'] = 'All Categories';


if($vid== 7){

$output['all-applicants'] = 'All Applicants';


foreach ($terms as $data) {

if($data->tid != 52 && $data->tid != 48 && $data->tid != 53){

$akey = strtolower($data->name);

$akey = str_replace(' ', '-',$akey);

$output[$akey] = $data->name;



return $output;


After you save the above code, go to block listing page and you’ll see a block search-filter.

Enable this block to the path you entered to your view. In This example my-site/gallery.

The final output should look something like this after some css changes.

Image removed.

And the URL will look like below.


Shakti SKJun 28, 2014