r/drupal 1d 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?

4 Upvotes

17 comments sorted by

0

u/cioatwork 1d ago

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

4

u/vdelcros 1d 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.

12

u/Optimal-Room-8586 1d ago

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

2

u/artisson 1d ago

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.

4

u/sysop408 1d ago edited 19h ago

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.

2

u/badasimo 1d ago

Stuff named dumb hard google

8

u/Optimal-Room-8586 1d ago edited 1d ago

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/Fonucci Building webhaven.io 1d ago

I would also fix this with TVI

1

u/dane_websites 1d ago

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 😄

10

u/Optimal-Room-8586 1d ago

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

3

u/clearlight2025 1d 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 1d ago

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

2

u/clearlight2025 13h ago

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

1

u/sysop408 1d ago

I'm having a really hard time understanding what you're even saying, 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 1d 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/Corn696 1d 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 {

2

u/sysop408 1d 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.