0

I have created a views with multiple exposed filters and I need another filter that is based on taxonomy.

My taxonomy structure is:

term 1

  • child 1.1
  • child 1.2

term 2

  • child 2.1
  • child 2.2

What I want is the following;

path: site.com/taxonomy/term/1

Exposed filter that can filter shows only;

  • child 1.1
  • child 1.2

How I have a filter that shows all taxonomy structure or when using depth it shows all child terms? Like this;

  • child 1.1
  • child 1.2
  • child 2.1
  • child 2.2

I'm sure there must be a way to only show the related child terms when on the parent term page.

thanks

0

You could use a form_alter hook in a custom module (or even your template.php file of your theme) to alter the filter values in the exposed view form.

as an example:

<?php
    function mymodule_form_alter(&$form, &$form_state, $form_id){
        //if it is the exposed filter view
        if (($form_id == 'views_exposed_form') && 
        //for the view we care about
        ($form_state['view']->name == 'your_view') &&
        //and the view display we care about
        ($form_state['view']->current_display == 'your_display') &&
        //and on a taxonomy term page
        (arg(0)=='taxonomy') && (arg(1)=='term') && (is_int(arg(2)))){
            //get the childern of the term
            if ($child_terms = taxonomy_get_children(arg(2))){
                //only care about the tids
                $child_terms_tids = array();
                foreach ($child_terms as $child){
                    $child_terms_tids[] = $child->tid;
                }

                //for the exposed filter, alter the available options
                foreach ($form['exposed_filter_name']['#options'] as $tid => $term){
                    //is is a child term?
                    if (in_array($tid,$child_terms_tids)){
                        //leave it
                    }else{
                        //remove it
                        unset($form['exposed_filter_name']['#options'][$tid]);
                    }
                }
            }
        }
    }

    ?>
  • unexpected "{" on line: (arg(0)=='taxonomy') && (arg(1)=='term') && (is_int(arg(2))){ – pindaman Oct 13 '12 at 9:58
  • I had missed an 'if' when typing up my example. updated the code. – Mike Miles Oct 16 '12 at 12:06
  • Thank you very much... still an error : if (arg(0)=='taxonomy') && (arg(1)=='term') && (is_int(arg(2)))){ Parse error: syntax error, unexpected T_IF – pindaman Nov 1 '12 at 12:47
  • ok I had added in if in the wrong place to fix the first issue. Updated, and ran the code myself with no issues. – Mike Miles Nov 1 '12 at 16:58

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.