Choosing a vocabulary for [term] in Token and Pathauto

Last updated on
30 April 2025

The Goal

When choosing a [term] token in one of your aliases, you want to exert some control over which term in which vocabulary is used. There is no way in the current version to specify to which vocabulary the term must belong: for node types with terms from multiple vocabularies one may be more "canonical" to the meaning than others.

Case in point: on my demo site for the G2 Glossary module, G2 definition nodes carry three visible vocabularies:

  • thematic context: things like storage, networking, machines...
  • grammatical nature
  • language

As you can guess, the thematic context is the one to be used by aliases: the entries from the other two vocabularies are interesting, but nowhere near meaningful enough to warrant appearing in the alias, whereas the context definitely adds semantic value to the URLs.

The Internals

(Skip if you're not interested in pathauto internals) Function pathauto_token_values starts by extracting a single $vid for the node being aliased, using db_result(db_query(some SQL .. ORDER BY v.weight, t.weight, t.name ... LIMIT 1));.

At this point, it only has a single vocabulary from which to choose a term, and it is not necessarily the one you want.

The Solution

The vocabulary selection query orders its result by vocabulary weight, so the term actually chosen by pathauto to build the alias is actually selected as the first term in alphabetical order among the lightest terms in the lightest vocabularies.

Which gives the needed solution: order the vocabularies used by the selected node type by weight so that the vocabulary from which you want to use terms for aliasing is the lightest one. You can find these weights at http://example.com/admin/content/taxonomy/edit/vocabulary/<vid>

Help improve this page

Page status: Not set

You can: