I currently have a taxonomy with the following structure

Make
-Model
--Type

I would like to create a views summary where if an argument is not provided it displays only the top level terms in the hierarchy (in this case the 'Make' terms). When I setup my summary view I can only get it to output all the terms in my vocabulary not just the top level terms. I found the following snippet of code that does what I would like, but I would rather be able to use views to accomplish this.

<?php
$vid = 3;
$terms = taxonomy_get_children(0, $vid);
if (count($terms)) {
  echo '<ul>';
  foreach ($terms as $term){
    echo "<li>" . l($term->name, taxonomy_term_path ($term)) ;
    $children = taxonomy_get_children($term->tid, $vid);
    $cnt = count($children);
    if ($cnt) echo " ($cnt)";
    echo "</li>";
  }
  echo '</ul>';
}
?>

Is this type of display possible using Views? Is there a way I could implement something like the code above into an argument handler? If so, what would that look like? Any help on this would be appreciated. Thanks

Comments

dawehner’s picture

i think this is not possible by default with views.

you could write your own taxonomy argument handler, which a specificed summary function which achive this.
but it will be hard.

see http://views.doc.logrus.com/group__views__argument__handlers.html and the file "modules/taxonomy/views_handler_argument_term_node_tid.inc".

syndicut’s picture

Status: Active » Fixed

Just discovered way to accomlish this:
1) Add argument Taxonomy: Parent Term
2) Set option "Action to take if argument is not present: Provide default argument" and set it to 0

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

jasom’s picture

Assigned: Unassigned » jasom

thank you

benlotter’s picture

Wow! An hour of searching to find a simple 3-line answer. Thank you so much. This works great!

I can't believe there are so many people out there asking this question and nobody had a Views-based answer -- except you. Thanks again.

benlotter’s picture

I've gotten a couple of private messages asking specifically how I got this type of view working. I'm posting my response here in hopes this will help others in the future as well.

View Type: Term
Arguments:
Taxonomy: Parent term / Action to take if argument is not present: Provide default argument / Default argument type: Fixed entry / Default argument: 0

Filters:
Taxonomy: Vocabulary = 'YourVocabName'

Style: Grid

Fields:
Taxonomy: Term image / Link this field to its taxonomy term page
Taxonomy: Term / Link this field to its taxonomy term page
Taxonomy: Term description

In case anyone is interested, the specific page that utilizes this is the Channels Page on my site (http://crossunion.com/channels).

dizarter’s picture

This works when validator is set to Basic Validation in which case view is expecting term ID.
So, passing a value of 0 returns all terms whose parent TIDs are 0, that is all root terms in the vocabulary.

However, this doesn't work when validator is set to Taxonomy Term and argument type is Term name/synonym converted to Term ID. In this case view is expecting Term Name, and passing 0 does nothing.

How to pass in default argument and make the view work in the 2nd case?

dizarter’s picture

Ok, got it working. Here is what works for me in case someone finds this thread looking for the solution.
In your view add Argument Taxonomy: Parent term.
Action to take if argument is not present -> Provide default argument -> Fixed Entry -> 0
Validator: PHP code and paste the code below

$arg = str_replace("-", " ", $argument);
$arg = ucwords($arg);
$myterms = taxonomy_get_term_by_name($arg);
if ($myterms[0]->vid == 14) { // replace 14 with the vocab you are matching against
  $handler->argument = $myterms[0]->tid;
}

The code above works wihtout vocab check, but it's not bad to make sure we are filtering against the right vocab.

NOTE:
Function taxonomy_get_term_by_name() returns an array of matching terms. In this case I was sure my term names were unique, thus $myterms[0] holds the term I am looking for.

If you have terms with same names you may want to run this array through foreach to find the exact term you need.

maksim24’s picture

Thank you very much too. syndicut, you are ninja :), your advice very simple but so good. I completle egry with benlotter

Katrina B’s picture

Is there a way to achieve this in Views in D7?

sprocketman’s picture

In Drupal 7, I was able to accomplish this by using 'Taxonomy term: Parent term' as a contextual filter, and then setting the default value (Fixed value) to NULL.

johnlinux’s picture

#6 is trully a life saver. Thanks

Doublesquare’s picture

simonbcfa’s picture

Issue summary: View changes

#13 Legend

joe huggans’s picture

#13 Thank you very much

shamsher_alam’s picture

#6 Worked for me.