Create a custom module with the following hooks.
//custom.module
function custom_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'views_exposed_form' && $form_state['view']->name == 'VIEW_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']);
// Unset the title and body filter
unset($form['title']);
unset($form['body_value']);
$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;
}
}
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',
);
}
}
}
Note: This solution will only work with Contains
operator and very case specific. It would be great if somebody will create a generic views filter handler to combine exposed filter as select list and allow search within selected filter.
Content: Title
andContent: Body (body)
together in a dropdown. so that i can select a field from the dropdown and search some string in that field using the textbox string.Content: Title
andContent: Body (body)
filter those will be exposed as textfield. right? How you suppose to combine them as dropdown?