Score:2

Pathauto alias full path for hierarchical taxonomy terms

nr flag

My client has content tagged by a hierarchical taxonomy of arbitrary depth.

Currently the maximum depth is three, but that could change:

ParentTerm
└── ChildTerm
    └── GrandchildTerm

Each Term has a unique TID (of course), but many Terms (especially descendants) share identical strings for Name.

However, the full Taxonomy tree path to each term is guaranteed to be unique (two adjacent Terms will never be identically named.)

All nodes tagged with GrandchildTerm of TID = 42 appear in a View at /taxonomy/term/42.

I would like to use Pathauto to generate aliases for each Term at the full taxonomy tree path, like so:

/ParentTerm/ChildTerm/GrandchildTerm

I was looking for Tokens to accomplish this.

I found instead that the paths are set by a View which generates the list of Nodes tagged with the Term ID.

The View has a contextual filter which accepts {{ arguments.tid }}.

Under Page Settings, the value for Path is /taxonomy/term/%.

Someone on this forum said setting up two contextual filters would help, but that does not apply here.

I do not want /taxonomyname/taxonomyterm as the path; I want /ParentTerm/ChildTerm/GrandchildTerm as the path (or, if necessary, /tags/ParentTerm/ChildTerm/GrandchildTerm).

It is possible to set a contextual filter Content: Has taxonomy term ID (with depth) but I haven't been able to get the path I seek this way.

Also, I would like a ParentTerm taxonomy page to return results for its descendants, but the current View only displays items for the specific TID. (The Views Tree module would help here, but it does not work with Drupal 9 yet.)

If this were not a requirement, I might try flattening the taxonomy and using term names like ParentTerm-ChildTerm-GrandchildTerm to differentiate identically named descendant terms. (This would solve another problem the client has: the ambiguity of selecting the correct term from the "Tags" vocabulary when tagging content.)

Score:2
in flag

I think you are really going to struggle to get this to work using the default taxonomy term page view. However, you can disable the view and then use the default entity full content display for terms and do whatever you'd like for it (just like a node page).

  1. Disable the taxonomy term display that controls /taxonomy/term/%tid. This will allow the term's entity canonical route to control itself.
  2. Create a pathauto alias for your terms with your desired pattern. I think you can probably use existing tokens for the parent piece of the path, something like [term:parent:url:path]/[term:name]. (Or just create a custom token if an existing token doesn't work.)
  3. Clone the display disabled in step 1 (as a block or embed - depending on how you want to place it) and place it on your taxonomy term pages (via block layout, context, preprocess, twig, or whatever). I don't think it'll be an issue, but if you're having trouble getting the correct tid set for the contextual filter using views' default options, you can use hook_views_pre_view() to get the tid from the route and pass it to the view programmatically.
hotwebmatter avatar
nr flag
Thanks @sonfd, I'll try that and accept your answer if it works out. What about returning results for descendants -- should I post that as a separate question?
sonfd avatar
in flag
@hotwebmatter - oops, sorry I missed that part. That's what the _Content: Has taxonomy term ID (with depth)_ contextual filter is for so you should be able to use that. I think if you get the rest working and are having trouble with that piece then it would be worth asking as a separate question.
hotwebmatter avatar
nr flag
I never got the hierarchical taxonomy to work the way I wanted it to and ended up taking another approach, but I'm accepting this answer because it was helpful.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.