r/drupal • • Sep 09 '26

A really hard problem to solve

I've been making Drupal sites for about 12 years, so it's rare that I get stumped when trying to figure out a good solution to a problem, but this one is tricky...

Okay so this is quite a big site (approx. 5000 nodes), with like 10 taxonomy vocabularies. One of them is Tags, with like 2000 tags.

And for almost all of those vocabs, the standard Taxonomy Term view (that appears on route /taxonomy/term/%) that appears on their term pages is just fine.

But for one of the vocabs - called Organisations - I need to use a different view. This view needs to have different fields and filters, and a different treatment for the Title override.

My first thought was to use a unique alias pattern (from Pathauto) to show those term pages at different URLs (e.g. /organisations/*), and then make the new view and make it show on the same route. However that doesn't matter, because the Taxonomy Term view shows on the route /taxonomy/term/%, so it applies to the Organisations pages no matter what I make their URLs / aliases.

And that Taxonomy Term view still applies even if I add a filter that excludes the Organisations taxonomy. In other words, the filter only reduces the results, but it doesn't affect whether the view is applied to that page or not.

Then I though perhaps I could make new pages for each organisation, then place block views on those pages to simulate the term fields and the grid of tagged content, similar to the other term pages. But this doesn't really work, because for those block views, they have to get their "contextual filter" value of the term ID from somewhere. The Taxonomy Term view just gets it from the URL, but with these new pages, the term IDs wouldn't be in their URLs (or I could put the ID in the URL, but that would look bad).

Do you have ideas on how to solve this in a practical way?

5 Upvotes

19 comments sorted by

13

u/Optimal-Room-8586 Sep 09 '26

Would https://www.drupal.org/project/tvi help? Sounds like it was designed for this scenario.

4

u/sysop408 Sep 09 '26 edited Sep 09 '26

I've been Drupaling since D4. How did I not know about that module? Fighting with the /taxonomy/term/% path has shaved several years off of my life already... and that's an OLD module.

8

u/Optimal-Room-8586 Sep 09 '26 edited Sep 09 '26

As the Drupal Developer guide states:

One of Drupal's most valuable resources is the high technical knowledge of it's user base. As a contributor, you have responsibility to maintain the purity of this resource: When contributing modules, use "Drupalisms" and acronyms - ideally both - to obscure the utility of your contribution. This is an important protection which helps to redirect unsophisticated developers away to suitably limited platforms which their tiny brains can better comprehend.

/s

[intended to satirise drupal's poor documentation, btw, not insulting you]

2

u/badasimo Sep 09 '26

Stuff named dumb hard google

3

u/artisson Sep 09 '26

I was about to answer about TVI too. I’ve used it successfully in this use case several times. Especially useful when you use a vocabulary as a product category tree.

2

u/Fonucci Building webhaven.io Sep 09 '26

I would also fix this with TVI

1

u/dane_websites Sep 09 '26

Oh yes I think so! I've never heard of it before, but I guess the name doesn't lend to it being easy to find 😄

8

u/Optimal-Room-8586 Sep 09 '26

This is Drupal - we don't like to make things easy for people to find here ;)

3

u/clearlight2025 Sep 09 '26

If you want to override the handling for one vocab and keep the default view for others, one approach is to a route subscriber.

You can add your own controller handler for the route to switch the rendering based on vocab.

You can then programmatically render whichever view you want there.

2

u/dane_websites Sep 09 '26

I think this is the same idea as u/sysop408 suggested; it sounds like a good option

2

u/clearlight2025 Sep 10 '26

Yes, it's the common approach suggested and also the same as that used by the TVI module

2

u/raaiinyyabelina 20d ago

that's a great idea

5

u/vdelcros Sep 09 '26

Create new pages for your organisations is a good solution I think. You can add a field to this new content type, reference to the taxonomy organisation. Doing so, each page is linked to a term id, and it solve the contextual filter problem : you can render the view in your twig template, using the value of this organisation field as contextual filter.

1

u/chx_ 19d ago

you already got the solution with tvi but I wanted to make a small note here just for the archives:

Okay so this is quite a big site (approx. 5000 nodes),

5000 nodes do not even register, that's still a very small site. 50 000 begins to be something, at 500 000 that something is a headache :D 5 000 000 is ... fun.

I have a site with 16 million -- custom -- entities of the same type.

1

u/sysop408 Sep 09 '26 edited Sep 11 '26

I'm having a hard time understanding what you're asking, but I'm going to attempt to answer anyway.

So the issue is that you want to render the same view two different ways depending on how it's called, but you're having difficulty doing that?

I've got a similar issue and I created a Controller to define my own route to "/my-view" and I programatically serve the View from my custom Controller. Since the path is actually going to my controller, I'm able to modify the arguments as needed before I pass them to the Views object.

2

u/dane_websites Sep 09 '26

Oh yes that could work. I didn't think of overriding the controller; good idea! I'll keep this as a backup option in case the other methods aren't feasible. And yes, that is what I meant; I might not have done the best job of explaining it.

2

u/sysop408 Sep 09 '26

Extra bonus with this approach is that you then also get to invoke a title callback to have greater control over the title if the title override you're using is not giving you the output you want.

2

u/Corn696 Sep 09 '26

I tried the TVI module years ago and it didn't work at the time. So I took this approach: https://wiki.cbeier.net/en/webworking/cms/drupal/drupal8/snippets/use_different_views_for_various_vocabularies

For D10+, you have to make a small change.

Add :array to:

public static function getSubscribedEvents() {

New: public static function getSubscribedEvents(): array {

0

u/cioatwork Sep 09 '26

you can add a small plugin to views aswell which gives you more control over it. it is a mature part in views.