Views Exposed Filter Search Clean URL’s for Drupal

Jun 28,2014/Posted by Shakti SK

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.

How to get clean URL’s for Views Exposed Filter Search pages?

Basically say your search view is generating dirty/junk URL like

mysite.com/gallery?categories=academic-program&type=finalist

I will help you get a clean URL like

mysite.com/gallery/categories/academic-program/type/finalist

Or

mysite.com/gallery/academic-program/finalist

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.

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();

break;

}

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.

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 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.

And the URL will look like below.

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

Tags: