0

I am using search api views for site search.

I have 4 content types A, B, C and D.

In the views filter when I expose a content type filter, it display as following on the search page

All
A
B
C
D

I want to combine the C and D content types.

All
A
B
Combined

Where Combined will be a custom text and when click , it should filter the content with Content type C and D.

Is it possible with search api or is there any workaround?

1 Answer 1

2

You have to:

1) allow multiselect for the field; 2) implements hook_form_views_exposed_form_alter and edit the filter - replace 2 values on new value and in validate function to replace back.

Example for the simple field:

/**
 * implements hook_form_views_exposed_form_alter
 */
function MYMODULE_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
    if($form['#id'] == 'views-exposed-form-list-your-views'){

        $form['#validate'][] = 'MYMODULE_form_views_exposed_form_validate';

        $form['field_your_field_value']['#multiple'] = false;
        $form['field_your_field_value']['#size'] = 1;
        $options = $form['field_your_field_value']['#options'];
        unset($options[3]);
        unset($options[4]);
        $options['3+4'] = 'name of combine value';
        array_unshift($options, '- ALL -');
        $form['field_your_field_value']['#options'] = $options;
    }
}

/**
 * our validate for  form views-exposed-form-list-your-views
 */
function MYMODULE_form_views_exposed_form_validate($form, &$form_state) {
    if($form['#id'] == 'views-exposed-form-list-your-views'){
        if(in_array('3+4', $form_state['input']['field_your_field_value'])){
            form_set_value($form['field_your_field_value'], array(3,4), $form_state);
        }
    }

}
2
  • Can you change this line if($form_state['input']['field_your_field_value'] == '3 + 4'){ to if(in_array('3+4', $form_state['input']['field_your_field_value']))? Then your answer is accepted answer.
    – Ahmad
    Feb 7, 2017 at 9:58
  • @Ahmad I've done it.
    – Alex
    Feb 7, 2017 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.