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.

views filters

Now in order to obtain a clean URL you’ll have to get into the back-end. 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();
break;
}
return $block;
}

The above code creates a block. Now inside block content I am going to call a function my_search_filter_view(). In this  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 to 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, I'm setting my variable to the URL argument type

$categories = my_selectbox_contents(2);
$types = my_selectbox_contents(7);

// Now I'm 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.

form_state_values_clean($form_state);

// And here is the value that was submitted:

if(!empty($form_state['values']['type'])){
$type= str_replace( ' ', '-',$form_state['values']['type']);
}else{
$type= 'all-applicants';
}
if(!empty($form_state['values']['categories'])){
$categories= str_replace(' ', '-',$form_state['values']['categories']);
}else{
$categories= 'all-categories';
}
$keyword ='';
if(!empty($form_state['values']['keyword'])){
$keyword =trim($form_state['values']['keyword']);
}
$search_path= 'gallery/2013/'.$categories.'/'.$type;
if(!empty($keyword)){
drupal_goto($search_path, array('query' => array('keyword'=>$keyword)));
}
else{
drupal_goto($search_path);
}
}

// 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 drop-down/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.

views block filters search

And the URL will look like below.

My-site/gallery/all-categories/all-applicants

I hope you have found this helpful. Click to know more about our Drupal services.

Contact us

LET'S DISCUSS YOUR IDEAS. 
WE'D LOVE TO HEAR FROM YOU.

CONTACT US