0

I am using D8. I have two views (articles and videos) sharing the same taxonomy (segments). How can I filter the views based on the view type? For example, if I am on the articles page, I would need to filter only articles when I select any of the taxonomy term. Right now, by default both articles and videos linked to the taxonomy term are shown.

I am using Taxonomy Menu to display the terms in a block.

4
  • Do you mean you have two Content Types? And not Views? I don't know how exactly Taxonomy Menu module integrates with the Views module, but you can easily build a new View that lists only one Content Type (this is a beginner tutorial for Drupal Views) and you can add the Filter for the Taxonomy term Reference, or even use the Summary option in a Contextual filter (advanced config) if you can't use Taxonomy Menu directly with your new Views. – prkos Oct 16 '20 at 22:37
  • Yes, I have 2 content types from which views are created. – builder0 Oct 17 '20 at 5:19
  • By default a taxonomy term link is always to the generic taxonomy listing, which includes all items tagged with the term regardless of type. The tricky part is changing the term URLs so they point to custom views instead of the default one. Are you comfortable with writing PHP? – Alfred Armstrong Oct 17 '20 at 14:38
  • Yes, I am comfortable with PHP. – builder0 Oct 17 '20 at 17:57
0

Term pages that list all the content that has been tagged with that term are built with "Taxonomy term" Views. You can edit that View to change what those pages show (/admin/structure/views/view/taxonomy_term).


Filter

The easiest change for you would be to add the Content type Filter (from the "Content" Category) to your Views so your visitors can choose manually which types to see listed.

Expose it to your visitors (this option is in the Filter Settings). This will show a filter at the top of each Taxonomy term page that will allow visitors to limit the content to only the chosen content types.

I recommend cloning (duplicating) the Taxonomy term Views, disabling the original and working on the duplicate so you can always refer back to it.


Contextual Filter

For a more automatic solution you can add a Contextual filter instead of a regular one (Contextuals are under Advanced View settings) to filter the results, and build a new Views Display to list the new taxonomy term links that are sensitive to the types of content.

There is already the "Has taxonomy term" contextual filter present there, you need to keep it.

Add another Contextual filter, named Content type, configure it with these options:
When the filter value is NOT in the URL
Hide view
When the filter value IS in the URL or a default is provided
Specify validation criteria
Validator Content type
Case in path Lower case
More
Transform spaces to dashes in URL

Rearrange the Contextuals so the type one comes up first, and the "Has taxonomy term ID" comes second.

Edit the "Has taxonomy term ID" Contextual filter and change its option under When the filter value is NOT in the URL from "Hide view" to "Display all results for the specified field".

This will enable you to add the content type into your path to have it automatically filtered by the type: /taxonomy/term/[ContentType]/[TID], for example: /taxonomy/term/article/5.

There is also an option to override the Title if you need to correct it to name only the content types filtered out.

To test that your Contextual filter is working you can enter the argument for it in the Preview with contextual filters: field and hit the Update preview button if you don't have it set automatically. To view all the results without filtering simply delete the argument or enter all at the Preview or at the end of your path. In this case Views takes 2 arguments so you need to include both of them, separated by /, e.g.: article/5, article/all.


List all links

You can now use Views to build a list of all links to your terms for each content type. It can be a Block Display where you override the "Has taxonomy term ID" to display a Summary, and adjust the Content type Contextual to provide a Fixed Default value, add another Display for your second content type (it doesn't have to be automatic since you only have two types).

There is more than one way of doing this, it's out of scope of this question, but it gives you enough to know which direction to take and learn more.

2
  • thanks for the detailed info. I will try as suggested by you. – builder0 Oct 19 '20 at 8:14
  • For now, I have added Segments as an exposed filter and placed that in one of the sidebars. – builder0 Oct 19 '20 at 8:15

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.