1

I have a news section of a Drupal 8 website that contains an ever-growing list of nodes. To avoid a messy menu in the long term, they do not have a menu position assigned but instead are shown via Views, which render News nodes tagged with certain taxonomy terms.

Because they aren't part of the main navigation, the regular Menu Block doesn't work here. Instead, I'm trying to replicate this part of the nav using the taxonomy term structure and exposing that in a View block. The terms IA matches the page IA, down to the News node level.

How can I expose the taxonomy term used on the Node, plus it's siblings and parent:

- newsroom
  - media release
    - 2019
    - 2020
    - 2021
  - records
  - blog entries

So a News node tagged with records should show this in the View block:

- newsroom
  - media release
  - records
  - blog entries

And a News node tagged with 2020 would show:

- media release
  - 2019
  - 2020
  - 2021

There are plenty of questions about exposing related Nodes to a certain term, but not related Terms on a Node...any help?

1 Answer 1

2

It is a common request to show reverse connection information through Views. It can be done with the help of reverse Relationships when starting from Taxonomy term type Views. You can find other answers with simple examples where you can learn about how it works. You are looking to list sibling terms, but commonly siblings are usually called nodes that share the same terms, so be mindful of the difference when reading those answers.

Your case is a bit more complicated because it involves more than one level of connections, and you want both cases to be solved with one Views. I don't know if it can be solved, I suspect it can with the Views Contextual Filters OR module but it still wouldn't be a trivial solution. It's out of scope of an answer here, so I'll just post as much as I can so you can take it to learn and expand on your own time.

This solution will list all three levels of Taxonomy tree for the node that has been tagged with the second level term, and will list third level siblings, (not second level as you'd requested). This requires a lot of tweaking to get it to work the way you want. first independently find the two solutions for each hierarchy level, then combine them with the Contextual OR option.


  1. Create Views of the type Taxonomy term and Block Display, Filter it to your Vocabulary, and you should have the Taxonomy Name Field there. Set the Pager to Display all items. So far it will list all the terms.

  2. Add a new Relationship from the Taxonomy term Category for the term Parent. It will be called Taxonomy term but you will see its Description explains it's the parent (search for "parent" when on the Relationships list to find it quickly). You can Require it since all your terms will have a parent (no nodes with only the grandparent)

  3. Add another Parent Relationship the same way, only make it use the previous Parent Relationship. This second one will introduce the Grandparent term information. Don't require it because some terms will be from the second level and won't have a grandparent so requiring it would exclude them (records)

  4. Add the third Relationship but this time the one for your Taxonomy reference field. It will be called something like Content using field_myTaxonomyField. Make it use the first Parent Relationship, you can Require it since all the relevant nodes will have that Taxonomy field filled in.

  5. Add the Field Term Parents and set it to Exclude from display. This will show the Parent term for each term but we're hiding it because we'll use it later for grouping. This can probably be done with the term Name Field that has the Parent Reationship applied.

  6. Add another Term Parents Field but this time make it use the Parent Relationship (the first one). This will show the grandparent term if it applies. Exclude from display because we'll use it for grouping later so don't need it repeated here.

  7. Under Format select HTML list and set the first grouping to use the Grandparent Field (Parent Field with Parent Relationship), and the second grouping to Parents field (Parent Field without Relationship). You will now see the grandchild terms grouped under their parents and grandparents.

  8. Lastly add the Contextual Filter ID from the Content Category, this will pass on the node ID to views to filter out only the terms referenced from this node. Configure the Contextual ID Filter:
    When the filter value is NOT available
    Provide default value
    Content ID from URL
    When the filter value IS in the URL or a default is provided
    Specify validation criteria
    Validator: Content
    Content type News
    Set the option for the case when the argument doesn't validate to something that makes sense to you.

    With the Contextual Filter present you will see no results unless you enter a node ID into the Preview field. Enter the number of a known node to test it's showing the correct terms and their hierarchy.

  9. Lastly save the Views and place the Block into a Region in your theme, and configure the Block Contextual option along with Visibility and other Block settings. Visit a node to test it's showing the hierarchy correctly

If the output doesn't look exactly as you'd like you can play with the Style settings and Override output in the Fields to get closer to what you need. In the end you can control it completely through twig templates. You might also want to look into Views tree.

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.