Altering views exposed filters with hook_views_query_alter()

To alter the form output we need to use form alter and overide array. We are going to add few options to filters that group filter options together, in this case we are dealing with list of countries.

/**

 * Implements hook_form_alter();
 *
 * @param $form
 * @param $form_state
 * @param $form_id
 */
function my_module_form_views_exposed_form_alter(&$form, &$form_state) {
    if($form['#id'] == 'views-exposed-form-warranty-admin-page-3' || $form['#id'] == 'views-exposed-form-warranty-admin-page-1') {

        $additional_options = array();
        $additional_options['- ROW -'] = '- ROW -';
        $additional_options['- South and Middle America -'] = '- South and Middle America -';
        $additional_options['- EMEA -'] = '- EMEA -';
        $additional_options['- US/Canada -'] = '- US/Canada -';

        if(isset($form['commerce_customer_address_country'])) {
            $all_options = $form['commerce_customer_address_country']['#options'];
            $merged = $additional_options + $all_options;
            $form['commerce_customer_address_country']['#options'] = $merged;
        } else if(isset($form['commerce_customer_address_country_1'])) {
            $all_options = $form['commerce_customer_address_country_1']['#options'];
            $merged = $additional_options + $all_options;
            $form['commerce_customer_address_country_1']['#options'] = $merged;
        } else {
            //no op
        }
    }

}

and after this we need to alter query conditions and group data there.

/**
* Implements hook_views_query_alter();
*
* @param $view
* @param $query
*/
function warranty_claim_views_query_alter(&$view, &$query) {
if(!isset($view->name)) {
return;
}

if($view->name == 'warranty_admin' && ($view->current_display == 'page_3' || $view->current_display == 'page_1')) {
$conditions = $query->where[1]['conditions'];

$count = count($conditions);
$index = -1;

for($i = 0; $i if($query->where[1]['conditions'][$i]['field'] == 'commerce_customer_profile_commerce_addressbook_defaults__field_data_commerce_customer_address.commerce_customer_address_country') {
$index = $i;
break;
}
}

if($index >= 0) {
$selected_value = $query->where[1]['conditions'][$index]['value'];

if(in_array('- ROW -', $selected_value)) {
$region_row = array(
'BD', 'BT', 'BN', 'KH', 'CN', 'IN', 'ID', 'JP', 'KZ', 'KP', 'KR', 'KG', 'LA', 'MY',
'MV', 'MN', 'MM', 'NP', 'PH', 'SG', 'LK', 'TW', 'TJ', 'TH', 'TM', 'UZ', 'VN', 'AU',
'FJ', 'KI', 'MH', 'FM', 'NR', 'NZ', 'PW', 'PG', 'SB', 'TO', 'TV', 'VU', 'TL', 'NC', 'AN');

foreach($region_row as $r) {
$query->where[1]['conditions'][$index]['value'][] = $r;
}

} else if(in_array('- South and Middle America -', $selected_value)) {
$region_south_america = array(
'AG', 'BS', 'BB', 'BZ', 'CR', 'CU', 'DM', 'DO', 'SV', 'GD',
'GT', 'HT', 'HN', 'JM', 'NI', 'PA', 'KN', 'LC', 'VC', 'TT',
'AR', 'BO', 'BR', 'CL', 'CO', 'EC', 'GY', 'PY', 'UY', 'PE',
'SR', 'VE', 'MX', 'AS', 'AI', 'AQ', 'SV');

foreach($region_south_america as $r) {
$query->where[1]['conditions'][$index]['value'][] = $r;
}

} else if(in_array('- EMEA -', $selected_value)) {
$region_emea = array(
'RU', 'AL', 'AD', 'AM', 'AT', 'BY', 'BE', 'BA', 'BG', 'HR',' CY', 'CZ', 'DK', 'EE',
'FI', 'FR', 'GE', 'DE', 'GR', 'HU', 'IS', 'IE', 'IT', 'LV', 'LI', 'LT', 'MK', 'MT',
'MD', 'MC', 'ME', 'NL', 'NO', 'PL', 'PT', 'RO', 'SM', 'RS', 'SK', 'SI', 'ES', 'SE', 'CH',
'UA', 'VA', 'AF', 'DZ', 'AZ', 'BH', 'EG', 'IR', 'IQ', 'IL', 'JO', 'KW', 'LB', 'LY', 'MA', 'OM',
'PK', 'AO', 'BJ', 'BW', 'BF', 'BM', 'CM', 'CV', 'CF', 'TD', 'KM', 'CD', 'CG', 'DJ', 'GQ', 'ER', 'ET',
'GA', 'GM', 'MR', 'MU', 'MZ', 'NA', 'NE', 'NG', 'MG', 'MW', 'ML', 'RW', 'ST', 'SN', 'SC', 'SL', 'ZA',
'SS', 'SD', 'TZ', 'TG', 'UG', 'ZM', 'ZW', 'SZ', 'GB', 'AX', 'AW', 'BV', 'IO', 'VG', 'BI', 'BQ', 'CW',
'DK', 'CY', 'GG', 'LU', 'QA', 'SA', 'SY', 'TN', 'TR', 'AE', 'EH', 'YE' );

foreach($region_emea as $r) {
$query->where[1]['conditions'][$index]['value'][] = $r;
}

} else if(in_array('- US/Canada -', $selected_value)) {
$region_us = array(
'CA', 'GL', 'US');

foreach($region_us as $r) {
$query->where[1]['conditions'][$index]['value'][] = $r;
}

} else {
//no op
}
}
}

}