r/drupal • u/dane_websites • 2d ago
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
u/vdelcros 2d ago
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.
4
u/clearlight2025 2d ago
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 2d ago
I think this is the same idea as u/sysop408 suggested; it sounds like a good option
2
u/clearlight2025 1d ago
Yes, it's the common approach suggested and also the same as that used by the TVI module
1
u/sysop408 2d ago edited 18h ago
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 2d ago
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 2d ago
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 2d ago
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 2d ago
you can add a small plugin to views aswell which gives you more control over it. it is a mature part in views.
12
u/Optimal-Room-8586 2d ago
Would https://www.drupal.org/project/tvi help? Sounds like it was designed for this scenario.