I have a taxonomy vocabulary with terms in hierarchical order. If I use an exposed filter, with level depth, all terms are listed, which are > 6,000. I want only the parent terms, which are only around 30.
I found here the following code (I adapted it to my case). It is an answered question, but obviously it does not apply efficiently to my case, because though it does what is suppose to do, it goes through every item and as a consequence is extremely slow (several seconds). Is there a better way that does not directly make a db_query to the taxonomy database tables or use a hook (PHP) ?
function mymodule_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
if($form['#id'] == 'views-exposed-form-search-page-page'{
foreach ($form['term_node_tid_depth']['#options'] as $term_key => $term) { // Check if this isn't the 'ANY' option
if($term_key !== 'All') {
// Check if this is a child by looking for '-' as first char in string
$term_value = reset($term->option);
if($term_value[0] == "-") {
unset($form['term_node_tid_depth']['#options'][$term_key]);
}
}
}
} // if
} // function