13

I have a vocabulary like this:

  • Parent 1
    • Child 1
    • Child 2
    • Child 3
  • Parent 2
    • Child 1
    • Child 2

Now I would like to use a view to create a list of a Parent term with its Child terms. I try to do this with Views UI, but only show the term parent... No the child term's. It is very confusing for me.

Can anyone help with this case? Thanks :D

13

Views can do this. Views is a UI for sql searches so you are setting the definition for the query that will be made of the content database.

  1. Create a view to display a taxonomy term with a page with a path configured along this line category/%. The % is a wildcard that will make the view respond to any value applied in the URL that matches this format.

  2. Set up your display and any filters desired.

  3. Define a relationship (third column of view UI page) and set it to taxonomy term: parent. This tells the view to look at the parent field in each taxonomy term searched. (Note that Drupal includes a field for each term that identifies its parent, but not its siblings or children).

  4. Next we tell the view to interpret the wildcard from the URL we used in the path for the view: Define a contextual filter (above the relationship selection) and set it to a taxonomy:termID. In the form, set the relationship to Parent in the drop down box. This tells View that the term being called in the URL will be applied as a filter for the 'parent' field of the taxonomy terms as they are searched.

  5. Finally we must complete refining the contextual filter. You can tell the view what to do if it does not find an answer in that section of the form if you want, but we must tell it what to do if it does find solutions in the query. To do so check the validation check box. Set the Validator to Taxonomy term and select the vocabulary that applies to your hierarchy. Then select either Term ID or Term name converted to term ID as the Filter value type. If the value you are passing in the % is a term ID, then you are done. If the passed value is a term Name, then you may want to check the box to convert dashes to spaces. IF you have multiple word term names, the URL will have dashes, which do not match the actual spaces in the term name. This little correction makes them agree so a test succeeds.

  6. Finally go to the preview area and enter the name or ID for a term you know has children and you should get the list of children terms. Use the sorting settings to arrange them in whatever order you need.

Good Luck!

  • Hello Ashlar, thank you for this explanation. Do you have any idea why taxonomy term: parent is not showing in the list of relationships? – znat Apr 21 '12 at 23:10
  • 2
    The identity of the parent is a field of the taxonomy term and the field is 'pid'. It is stored there as a term number reference, so it is already there, but only as a reference. – Ashlar Apr 22 '12 at 20:42
  • 1
    I find "Taxonomy term: Parent term" in Relationships, but I can't add the parent ID in the "Field" section. I want to show a table with the parentID in a column (if it exists). – Nic Nov 20 '12 at 11:28
7

I have archive this use view too, but in slightly different way compare Ashlar.

  1. Add Taxonomy term: Term ID in CONTEXTUAL FILTERS.
  2. Add Taxonomy term: Parent term in RELATIONSHIPS. (leave Require this relationship unchecked.)
  3. Add Taxonomy term: Parent term in filter section. select great than 0.
  4. do not forget to filter your taxnonmy term in mycase Taxonomy vocabulary: Machine name (= Business Directory).

  5. add Taxonomy term: Rendered Taxonomy term to your fields , select relationship to parent , check the Exclude from display.

  6. In setting, under FORMAT section. In grouping field 1, select (Parent) Taxonomy term: Rendered Taxonomy term.

You should good to go. you can use view tpl to add more class base on the group. I have enclose my screenshot for references. Sorry for my English.

enter image description here

  • did not work for me. Shows all not-top-level taxonomy terms. – Riki137 Dec 1 '14 at 13:34
0

If you'd like to use that view as a field, you can create such a list without using Views, by using Taxonomy Term Reference Filter by Views module and you can specify the parent, so the list can be filtered automatically.

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.