Score:2

How to create a view that lists all taxonomy terms of a vocabulary, filtered by a term id?

cn flag

I have a lot of different vocabularies. On The taxonomy page (the standard view provided) I would like to add a block (view) that takes the current taxonomy id from the url/page and displays all taxonomy terms belonging to the same vocabulary. I tried adding a contextual filter, but when I choose term id from url, the view only shows one term, but I would like to show all terms of the vocabulary the term belongs to.

Of course I could create a block for each vocabulary, but as I have a lot, I'm searching for a more dynamic approach.

For Example: on the page /taxonomy/term/39 I would like to add a block (view) that takes the taxonomy id 39 and lists all terms of the vocabulary the id 39 belongs to.

berliner avatar
bd flag
Welcome to Drupal Answers! You should provide more context for this question, e.g. your views configuration, so that users here can better understand your problem.
berliner avatar
bd flag
From the top of my head, I'd say you also need a relationship to the parent vocabulary in order to pull in other terms. But it's a bit difficult to say without seeing your views configuration.
cn flag
I tried to clarify and edited my question
berliner avatar
bd flag
I just played a bit around with views and it seems to me that you can't do what you want with Drupal core. There is no option to add a relationship from a term to a vocabulary and no option either to retrieve the vocabulary from the current term page. So either there is a contrib module that solves this that I couldn't find, or you need to do some custom coding to achieve this.
Score:1
cn flag

I created a views argument_default plugin that did the trick:

public function getArgument() {
  if (($taxonomy_term = $this->routeMatch->getParameter('taxonomy_term')) && $taxonomy_term instanceof TermInterface) {
    $vocabulary = $taxonomy_term->bundle();
    return $vocabulary;
  }
}
berliner avatar
bd flag
Nice! Glad you found a solution.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.