0

I have multiple content types and would like to create a couple of views to show various content types as sub directory to the term page.

The challenge is that I need to the path to be taxonomy/term/%/content-type1 taxonomy/term/%/content-type2

Tired using tokens in the views path. It didn't work.

Any other suggestions?

1 Answer 1

6

The way to influence filtering Views through URL arguments is to use Contextual filters. For each URL sensitive Contextual filter you get one argument in your URL (separated by /).

In your case you need to add a new Contextual Filter of type Content: Content type and configure it:
When the filter value is NOT in the URL
Provide default value
Raw value from URL
Path component: 4
When the filter value IS in the URL or a default is provided
Specify validation criteria
Content type
Single ID
Case in path: Lowercase
More
Transform spaces to dashes in URL

Change the Path of your Page from taxonomy/term/% to taxonomy/term/%/%.

Now when you visit taxonomy/term/1/article it will list all articles tagged with the term 1 (I'm assuming you have cloned the default Taxonomy term Views).


Show term names instead of TID in URL

It is possible to pass term names to Views from URL, instead of using term ID numbers. So the URL might look something like: taxonomy/term/tag1/article

To achieve that we have to introduce the term name into Views. In this example we cloned the default Taxonomy term Views, and they only have term ID available.

To introduce more information about the taxonomy term we add a new Relationship. In this case we add Taxonomy term referenced from field_myvocabulary (use your Entity reference field referencing the term from the relavant vocabulary).

There are no consequences of this step visible from the outside, but from the inside we have more choices to choose from when adding new Contextual Filters. One of those new choices is Name from Taxonomy term Category. Add that as a new Contextual Filter and configure it:
Relationship: field_myvocabulary: Taxonomy term
Provide default value
Raw value from URL
Path component: 2
When the filter value IS in the URL or a default is provided
Specify validation criteria
Taxonomy term name
Vocabulary: My Vocabulary
Transform dashes in URL to spaces in term name filter values
More
Transform spaces to dashes in URL

Rearrange the Contextual filters to have that Name filter first, then the Content type second and you can ignore or remove the original "Has taxonomy term ID" in third place.

Now you can enter tag1/article into the Preview and you'll see the results, all with using human-friendly URL bits. or visit /hub/tag1/article on your site.

Relationships can introduce duplicate results. If this happens make sure your filters have turned on "Reduce duplicates" option, although you'll probably have to edit your Views Query settings and set them Distinct.

Be careful about the Case settings and converting spaces to dashes and vice versa. Your term name might be "Best term", and depending on your Contextual filters settings you might need to enter "best term" or "Best-term" etc.

The default Taxonomy term Views renders Taxonomy term in Views Header area. I haven't been able to make it work with the Taxonomy name though. It does work if you keep the ID as the third filter and use path tag1/article/1.

But you can solve it (if you need this feature of rendered Taxonomy term in Views Header) with the help of an additional module: Views taxonomy term name into ID and the patch mentioned there.

You can also use this module to get term name in URL, instead of using a Relationship like explained above ;)

11
  • Yes, prkos. I have cloned taxonomy term. Will give this a try and let you know.
    – Progonat
    Jun 13, 2018 at 18:51
  • One question. Should the default contextual filter stay or be removed?
    – Progonat
    Jun 13, 2018 at 18:59
  • It should stay, the first Contextual filter will pass on the information about which Taxonomy term to display on the page. That is the first % in your URL. The second % in your URL is from the second Contextual filter, in your case the content type machine name. So you need them both working to get the pages you want (term pages with content type subpages).
    – prkos
    Jun 13, 2018 at 20:17
  • 1
    I'm glad you got it working! What exactly do you mean use token to link? Do you mean instead of showing the TID to show the name of the term? It's difficult to say before I know the details of what you'd like to end up with. What would be your ideal path pattern?
    – prkos
    Jun 14, 2018 at 17:50
  • 1
    I added the term name solution to my answer ;) BTW if you also want hub/termname to work on its own, without the content type as third argument, you have to create 2 pages, one for each depth. You can also use Menu option under Page settings to show your subpages as Drupal tabs on the Parent pages.
    – prkos
    Jun 14, 2018 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.