Created
August 3, 2014 07:00
-
-
Save msankhala/efe0b9ff7e0b7e75b8b0 to your computer and use it in GitHub Desktop.
Chose the exposed filter from a list of dropdown exposed filter and search within that selected filter with the search term entered in the same textfield. https://www.dropbox.com/s/jgenr05lo8r3173/Voila_Capture%202014-08-01_11-01-15_pm.png 1) Create a view with title and body field.
2) Create two filter with title and body with operator 'Contain…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_FORM_ID_alter(). | |
*/ | |
function custom_form_views_exposed_form_alter(&$form, &$form_state, $form_id){ | |
if ($form_id == 'views_exposed_form' && $form_state['view']->name == 'VIEWS_NAME') { | |
// Unset the title and body filter info so that it doesn't print any label. | |
unset($form['#info']['filter-title']); | |
unset($form['#info']['filter-body_value']); | |
// Set the #printed = TRUE on title and body render element to escape them from being print. | |
$form['title']['#printed'] = TRUE; | |
$form['body_value']['#printed'] = TRUE; | |
$form_element = array(); | |
$options = array( | |
// '' => '-Select-', | |
'title' => 'Title', | |
'body' => 'Body', | |
); | |
$form_element['search_within'] = array( | |
'#type' => 'select', | |
'#title' => t('Search within'), | |
'#options' => $options, | |
'#default_value' => !empty($form_state['search_within']) ? $form_state['search_within'] : '', | |
'#description' => t('Set the <em>Title</em> or <em>Body</em> within which you want to search.'), | |
'#prefix' => '<div class="views-exposed-widget">', | |
'#suffix' => '</div>', | |
); | |
$form_element['search_box'] = array( | |
'#type' => 'textfield', | |
'#title' => t('Search term'), | |
'#default_value' => !empty($form_state['search_box']) ? $form_state['search_box'] : '', | |
'#description' => t('Enter the term which you want to search.'), | |
'#prefix' => '<div class="views-exposed-widget">', | |
'#suffix' => '</div>', | |
); | |
$form = $form_element + $form; | |
} | |
} | |
/** | |
* Implements hook_views_query_alter(). | |
*/ | |
function custom_views_query_alter(&$view, &$query) { | |
if ($view->name == 'VIEWS_NAME') { | |
if (!empty($view->exposed_input['search_box']) && $view->exposed_input['search_within'] == 'title') { | |
// Add title filter condition if title is selected | |
$query->where[1]['conditions'][] = array( | |
'field' => 'node.title', | |
'value' => '%' . $view->exposed_input['search_box'] . '%', | |
'operator' => 'LIKE', | |
); | |
} | |
if (!empty($view->exposed_input['search_box']) && $view->exposed_input['search_within'] == 'body') { | |
// Create join with fiedl_data_body table. | |
// source: | |
// https://api.drupal.org/api/views/includes!handlers.inc/function/views_join%3a%3aconstruct/ | |
// https://www.drupal.org/node/2049051#comment-7784967 | |
$join = new views_join; | |
$extra = array(); | |
$extra[] = array( | |
'table' => 'field_data_body', | |
'field' => 'entity_type', | |
'value' => 'node', | |
'operator' => '=', | |
); | |
$extra[] = array( | |
'table' => 'field_data_body', | |
'field' => 'deleted', | |
'value' => '0', | |
'operator' => '=', | |
); | |
$join->construct('field_data_body', 'node', 'nid', 'entity_id', $extra); | |
// Add join to query; 'node' is the left table name | |
$view->query->add_relationship('field_data_body',$join,'node'); | |
// Add fields from table (or where clause, or whatever) | |
$view->query->add_field('field_data_body','entity_id'); | |
// Add body filter condition if body is selected | |
$query->where[1]['conditions'][] = array( | |
'field' => 'field_data_body.body_value', | |
'value' => '%' . $view->exposed_input['search_box'] . '%', | |
'operator' => 'LIKE', | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment