OK. I have Pathauto and Token

I have the following Tag Structure:

Drinks > Beer

I want to use path auto to make all nodes tagged with Drinks or Beer to have a URL of /Drinks/[Node:title]

So essentially I can use [node:field-tags:0:parent]/[Node:title] to get it to work correctly for all nodes tagged as Beer.

However if the node is tagged as Drinks then the parent does not exist and it will just return the node title. So in that case I would want to use [node:field-tags:0:name]/[Node:title]

So what I am looking for is something like this (if it exists):

[node:field-tags:0:parent ELSE node:field-tags:0:name]/[Node:title]

I have looked into custom tokens and can't figure it out. I was hoping there was a simple way to do this.

1 Answer 1


Ok I figured it out. Use the Custom Token Module. Turn on PHP Filter in Core Drupal Modules Create a new Custom Token in structure Choose Nodeds in the Token Type then use this code:

if (isset($data['node'])) {
    $parent = token_replace('[node:field-tags:0:parent]', $data);
    return ($parent != '[node:field-tags:0:parent]') ? token_replace('[node:field-tags:0:parent]', $data) : token_replace('[node:field-tags:0:name]', $data);

It will return the parent tag of the first tag in the list and if none exist it will return the first tag in the list

Your Answer

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

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