1

In Drupal 8, how can I display a taxonomy term's immediate children on its page?

For example, I have a hierarchical taxonomy vocabulary like this (simplified):

Animals
--Fishes
----Blennies
------Yellow Blenny
------Green Blenny
----Gobies
------Blue Goby
------Red Goby

On the page for the taxonomy term "Fishes" (e.g. drupal/fishes) I would like Blennies and Gobies (but not their children) to appear.

I have tried configuring Contextual Filters in Structure>Views>Advanced but not been able to do this.

  • Do you need to show content that references the child terms as well or is it for showing the terms only? – Mario Steinitz Dec 3 '17 at 22:43
  • @MarioSteinitz I need to show content that references the child terms, in particular an image relating each child term with a caption and possibly some other information. – Nick Hope Dec 4 '17 at 17:39
1

Given your additional information, this short step-by-step guide, that should give you the desired results:

  • Duplicate/disable the original Taxonomy view / alter it as follows.
  • In "Advanced/Contextual filters" remove "Content: Has taxonomy term ID"
  • Add a new Contextual filter "Content: Has taxonomy term ID (with depth)":
    • Depth: 1 (will show content for the next level child terms as well)
    • When the filter value is NOT in the URL:
      • Provide default value
      • Type: Taxonomy term ID from URL
      • Load default filter from term page: checked
    • When the filter value IS in the URL or a default is provided:
      • Override title: checked, use the variable {{ arguments.term_node_tid_depth }} (this will make sure the term name will be shown in the page title)
      • (Optionally, but strongly suggested) Specify validation criteria: checked
      • Validator: Taxonomy term ID
      • Vocabulary: (your vocabulary name checked)
      • (Optionally) Validate user has access to the taxonomy term: checked

As you'd like to show an image, you may also wish to change the Format of the view from Content to Fields and add the image field of your tagged content.

Please note, that the depth modifier may lead to duplicates. So within the Advanced / Other section's Query settings, you should enable the "Distinct" option.

You can further improve your view results by adding relations to the actual terms, the term name and adding grouping. But this seems out of scope for your initial question.

I did the above steps on a clean D8 test instance with auto-generated content and vocabularies, and it was working fine.

EDIT:

According to your comments, you actually like to show fields of the child taxonomy terms only. So here is how to do that:

  • disable the default taxonomy view
  • create a new view
  • in View settings / Show choose "Taxonomy term"
  • "of Type" is your vocabulary
  • in "Page settings" check "Create a page" and provide the URL "taxonomy/term/%"
  • click "Save and edit"
  • in Advanced / Contextual filter add the filter "Taxonomy term: Parent"
  • apply the same settings as in my above solution to the contextual filter, except for the title variable, which should now be '{{ arguments.tid }}'

As result, you should see the first level children of the taxonomy term in the view.

If you like to see some of their properties only, switch the type of the view to fields and add the field you like to display.

  • Thank you. I tried these steps a couple of times but unfortunately still no children appear. You can see my steps in my video here. The description of the taxonomy term also disappears, having been shown in the default taxonomy term view. – Nick Hope Dec 5 '17 at 10:25
  • Spent some minutes to watch your movie. ;) - Do you actually have Content in your demo system that has these terms assigned? Be aware, that this view is a content view. It will show content only, if you have some that is using this term. Otherwise it still keeps being empty. – Mario Steinitz Dec 5 '17 at 10:31
  • For the description text, make sure the header block gets the argument '{{ raw_arguments.term_node_tid_depth }}'. – Mario Steinitz Dec 5 '17 at 10:33
  • No, in this test project I don't have content that has those terms assigned. I was hoping it would display the terms themselves. I will add a content page for each term and then try again. Thanks. (Actually this is an issue I've been unsure about... whether to build my site using the taxonomy terms themselves as my display pages, or whether to build a content page relating to each. This issue may be making up my mind for me.) – Nick Hope Dec 5 '17 at 10:39
  • So this is your real issue. Taxonomy terms are usually used to tag other content or other entities. That's why the default taxonomy view is showing all content (pages, articles, ...) that has the term assigned you are currently watching. – Mario Steinitz Dec 5 '17 at 10:42
0

In the view :

if your are modifying/duplicating the default taxonomy_term view, you should remove the default contextual filter (has taxonomy term id) and add a new one "Has taxonomy term ID (with depth)", with a depth of 1.

Programmaticaly :

$terms = \Drupal::service("entity_type.manager")->getStorage("taxonomy_term")->loadTree("<VID>","<TID of Fishes>", 1 /* the depth : 1 */, true /* to load entities */);
foreach($terms as $term){
  //rendering whatever...
  print $term->label();
}

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.