Pathauto - Create node URL from taxonomy hierarchy

By xngo on January 19, 2020

In Drupal, you can create a nested hierarchy with your taxonomy terms. For my case, I have the following vocabulary structure.

Drupal 8 - Taxonomy structure

I want the URL to look like https://example.com/canada/quebec/montreal.

By default, the [node:field_ho_location:entity:parents] token does provide all the parent terms.

Drupal 8 - Pathauto - Pattern

However, it will concatenate each parent term with an hyphen and will generate URL like https://example.com/canada-quebec/montreal.

Luckily, token module does provide function to manipulate the results. I used :join-path function to replace the hyphen with a forward slash. So, my final pathauto pattern looks like this.

[node:field_ho_location:entity:parents:join-path]/[node:field_ho_location:entity:name]

The URL generated is https://example.com/canada/quebec/montreal.

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.