0

I am using a Simple Hierarchical Select as an exposed filter in views.

How would I hide terms that have no nodes from the select box?

1

There is no simple way to do that without writing custom module to eliminate the terms you wanted. That said, you can write hook_form_alter to achieve this.

/**
 * Implements hook_form_alter
 *
 **/
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {          
  if ($form_id == 'views_exposed_form' && $form['#id'] == 'YOUREXPOSEDFORMID') {            
    foreach ($form['field_tags_tid']['#options'] as $tid => $term_name) {
      if (is_numeric($tid)) {
        $result_count = db_select('taxonomy_index', 'ti')
        ->fields('ti')
        ->condition('ti.tid', $tid, '=')
        ->execute()
        ->rowCount();

        // Removing the option for tags with no-content
        if (!$result_count) {
          unset($form['field_tags_tid']['#options'][$tid]);
        }                
      }
    }
  }
}

NOTE: Here, I assumed field_tags_tid as the field name, should be replaced with the appropriate field name.

| improve this answer | |
  • Thank you very much! I got this code to work with a regular views filter (select box). How ever I am having huge trouble getting the right field name for thge Simple Hierarchical Select. I always get error: "Notice: Undefined index.." and code doesn't work. So your code works.. maybe I should ask new question on how to get proper field name? – puudutus Mar 31 '16 at 14:00
  • To be more specific.. I think I found the right filed name, but now the error is "Notice: Undefined index: #options" – puudutus Apr 1 '16 at 9:33

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.