0

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
1

I had a situation like this, i done it by giving a custom options array to the exposed filter in hook_form_alter().

 function mymodule_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == "views_exposed_form" && $form['#id']=='your-form-id' ){
 $tid_options = get_tid_options();

 $form['your-field-name']['#type'] ='select';
 $form['your-field-name']['#size'] = null;
 $form['your-field-name']['#default_value'] = '';
 $form['your-field-name']['#options'] = $tid_options;
   }
}


function get_tid_options() {
  $cc=array();
  $tax=taxonomy_vocabulary_machine_name_load('machine-name');
  $vid= $tax->vid;
  //Code to find parent terms here according to your vocabulary structure.
  $child_terms = taxonomy_get_tree($vid);
  foreach($child_terms as $values) 
  $tid=$values->tid;
  $name=$values->name;
  $cc[$tid] = $name; 
 }
return $cc;

}

Use taxonomy_get_parents or taxonomy_get_parents_all to find parent terms.

May be this will help you..

  • Thanks for your reply. I have used taxonomy_get_parents ($value->tid) before, I was out of my mind by saying to get the values at low-level database. In any case, to be honest, only a major change of Drupal version could make such a hard code fail. I really meant if there is another high-level solution, i.e., a configuration in Views UI, another module or submodule, etc. Thanks!! – Cesar Sep 22 '15 at 15:11
0

For listing the zero-level (parent) (or others), it is best to use the Drupal function taxonomy_get_tree

function get_options_of_custom_form() {
    $terms = taxonomy_get_tree(2, 0, 1);
    $options = array();
foreach($terms as $key => $value){
    $options[$value->tid] = $value->name;
} // foreach
  return $options;
} // function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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