Drupal view – customize autocomplete taxonomy term filter to show only child terms by attaching view as data source
Problem: We have a taxonomy terms as below with parent child terms and child terms under parents. We can say this taxonomy as categories with below terms.
We have another view called article list where we have added this taxonomy as filter with autocomplete and exposed to user, but when we start type ‘pa’, it will display all terms as below.
This view has below taxonomy term filter exposed.
So in this article we are discussing how to remove parent terms and only keep only child terms in suggestion.
For this first create view of taxonomy terms and add a display of entity reference.
So in this view add filter criteria as below for each parent.
So our filter criteria will be as below.
So now output of this view will be showing all child terms without parent terms.
So we are using this view as a source of terms in autocomplete taxonomy term field in our Article view filter.
Now we have to override existing view exposed form in our custom module.
In your custom module .module file create hook form alter.
function your_module_form_alter(&$form, &$form_state, $form_id){
}
Then iside above function, check for view exposed form.
if($form['#id'] == 'views-exposed-form-article-list-page-1'){
}
Here views-exposed-form-article-list-page-1 is article list views exposed form.
If we print this form as below you can see term id element.
echo "<pre>"; print_r($form); echo "</pre>";exit;
So the term field will be like below.
[tid] => Array
(
[#type] => entity_autocomplete
[#default_value] =>
[#target_type] => taxonomy_term
[#selection_settings] => Array
(
[target_bundles] => Array
(
[0] => categories
)
)
[#tags] => 1
[#process_default_value] =>
)
No we have to override this form autocomplete data with our view output we created for showing only child terms.
By default Drupal Core autocomplete class populates all taxonomy terms in autocomplete field while user searching for terms.
In if condition, override this array as below.
$form['tid'] = [
'#type' => 'entity_autocomplete',
'#title' => t('Search '),
'#target_type' => 'taxonomy_term',
'#tags' => TRUE,
'#multiple' => TRUE,
'#selection_handler' => 'views',
'#selection_settings' => [
'view' => [
'view_name' => 'category_list',
'display_name' => 'entity_reference_1',
'arguments' => []
]
]
];
In addition to other keys, we have added ‘#selection_handler’ => ‘views’.
In #selection_settings we are providing view array with details of the category_list view like view machine name and display machine name.
So the final code of the form alter will be look like below.
function dn_viewalter_form_alter(&$form, &$form_state, $form_id){
if($form['#id'] == 'views-exposed-form-article-list-page-1'){
if(isset($form['tid'])){
$form['tid'] = [
'#type' => 'entity_autocomplete',
'#title' => t('Search '),
'#target_type' => 'taxonomy_term',
'#tags' => TRUE,
'#multiple' => TRUE,
'#selection_handler' => 'views',
'#selection_settings' => [
'view' => [
'view_name' => 'category_list',
'display_name' => 'entity_reference_1',
'arguments' => []
]
]
];
}
}
}
Clear the cache and reload the article list page . while you type ‘Pa’ you can see only child elements in autocomplete.
You can down sample module source code here.