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
Comment #1
dawehneri 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".
Comment #2
syndicut commentedJust 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
Comment #4
jasom commentedthank you
Comment #5
benlotter commentedWow! 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.
Comment #6
benlotter commentedI'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).
Comment #7
dizarter commentedThis 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?
Comment #8
dizarter commentedOk, 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
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.
Comment #9
maksim24 commentedThank you very much too. syndicut, you are ninja :), your advice very simple but so good. I completle egry with benlotter
Comment #10
Katrina B commentedIs there a way to achieve this in Views in D7?
Comment #11
sprocketman commentedIn 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.
Comment #12
johnlinux commented#6 is trully a life saver. Thanks
Comment #13
Doublesquare commentedBelow Link helped me lot with picture
http://intopweb.com/content/listing-only-parent-taxonomy-terms-views-exc...
Comment #14
simonbcfa commented#13 Legend
Comment #15
joe huggans#13 Thank you very much
Comment #16
shamsher_alam commented#6 Worked for me.