By default, when adding filter criteria for taxonomy, views provides an autocomplete widget for entity reference fields with no match operator set, so it defaults to 'Contains'. At the time of this article, there was no option in the Views admin UI to change this.
For a project, I had a very large country and state taxonomy. My views exposed form had options to filter by both country and state. The autocomplete results matching with "Contains" were less than useful. I tried typing "usa" for example, and USA was nowhere in sight.
- Korea, Republic of » Busan Gwang'yeogsi
- Palestine, State of » Jerusalem
- Zambia » Lusaka
- Macedonia » Mavrovo i Rostusa
- Oman » Musandam
- Indonesia » Nusa Tenggara Barat
- Indonesia » Nusa Tenggara Timur
- Namibia » Omusati
- Azerbaijan » Qusar
- Italy » Ragusa
A better match operator in this context would be "Starts With", which gave me
- USA
- Turkey » Usak
Solution
A simple fix with a form hook can override the match operator.
function my_module_form_alter( &$form, &$form_state, $form_id ) {
if ($form_id === 'views_exposed_form' && $form['#id'] === 'views-exposed-form-my-view-page-1') {
// change match operator for country
$form['field_country_target_id']['#selection_settings'] += ['match_operator' => 'STARTS_WITH'];
// change match operator for state
$form['field_state_target_id']['#selection_settings'] += ['match_operator' => 'STARTS_WITH'];
}
}