1

I have a view with (Taxonomy Reference Field) as Exposed Filter, The vocabulary contain parent terms and some child terms, I want to see the first level terms only and remove any children if any.

Currently It look like this

Exposed Filter with Childes

I want to remove the Keyboard Type 1, Type 2 and so on.

I was thinking of using a hook_form_alter, but not sure about the exact usage to remove any child term.

2 Answers 2

3

Here is my solution which I think is quite a bit simpler. Simply remove the entries from the form if they start with a '-'. Note that you must have the 'Show hierarchy in dropdown' option selected for the children to begin with a '-' hyphen.

/**
* Implements hook_form_alter().
*/
function mymodule_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {

  if($form['#id'] == 'views-exposed-form-[your-view-here]') {

    foreach ($form['tid']['#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['tid']['#options'][$term_key]);
        }
      }

    }

  }

}
1
  • I did the same, but I forgot to add the answer myself. Thanks :) Oct 23, 2013 at 20:07
0

A bit late answer but you can use hook_form_alter as you stated.

$localities = get_all_localities($term_id);
$localities_tids = array();

foreach ($localities as $tid => $name){
    $localities_tids[] = $tid;
}

//alter the available options in the exposed filters
foreach ($form['term_node_tid_depth_i18n']['#options'] as $tid => $term){
    if ( !in_array($tid, $localities_tids) ){
        if ( $tid != 0 && !empty($tid) ) {
            unset($form['term_node_tid_depth_i18n']['#options'][$tid]);
        }
    }
}

In this example I get all the localities (child elements) with parent $term_id, after that just compare the new tid's list with the id present on the form element and remove what u don't need using unset()

Will also leave you my example to get the child localities from the region parent id ($term_id).

function get_all_localities($regiao_selecionada) {
    $lang_code = get_site_lang();
    $loc_list = array();

    if ( !empty( $regiao_selecionada ) && $regiao_selecionada != '0' ) {
        $locs = db_query("
            SELECT td.tid, 
            COALESCE((SELECT lt.translation FROM locales_source ls, locales_target lt WHERE ls.lid = lt.lid AND ls.context LIKE CONCAT('term:',td.tid,':name') AND lt.language LIKE '$lang_code' LIMIT 1), td.name) as 'name', th.*  
            FROM taxonomy_term_data td, taxonomy_term_hierarchy th 
            WHERE th.tid = td.tid AND td.vid = 5 AND th.parent = $regiao_selecionada AND td.name <> 'Foreign' AND td.name <> 'Nenhuma' AND td.tid NOT IN (select tid from taxonomy_term_hierarchy where parent IN (select tid from taxonomy_term_data td where td.name = 'Foreign' OR td.name = 'Nenhuma'))
            ORDER BY td.name ASC
        ");
    } else {
        $locs = db_query("
            SELECT td.tid, 
            COALESCE((SELECT lt.translation FROM locales_source ls, locales_target lt WHERE ls.lid = lt.lid AND ls.context LIKE CONCAT('term:',td.tid,':name') AND lt.language LIKE '$lang_code' LIMIT 1), td.name) as 'name', th.*  
            FROM taxonomy_term_data td, taxonomy_term_hierarchy th 
            WHERE th.tid = td.tid AND td.vid = 5 AND th.parent <> 0 AND td.name <> 'Foreign' AND td.name <> 'Nenhuma' AND td.tid NOT IN (select tid from taxonomy_term_hierarchy where parent IN (select tid from taxonomy_term_data td where td.name = 'Foreign' OR td.name = 'Nenhuma'))
            ORDER BY td.name ASC
        ");

    }


    foreach ($locs as $id => $loc) {
        $loc_list[$loc->tid] = $loc->name;
    }

    return $loc_list;
}

Hope this can help you out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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