1

I am trying to create URL path aliases patterns with Tokens and Pathauto.

My main issue is how to I get the child of a parent of a taxonomy term? So my url is something like this.

[child-term:name]/[node:name]

In tokens help page I can see a lot [term:parent] references but not child ones. I am sure is something simple but in the vast amount of tokens I cannot seem to find it.

0

You can do quite advanced things with hook_pathauto_alias_alter to replace complex aliases patterns with just PHP code.

Here's how to use it:

/**
 * Implements hook_pathauto_alias_alter().
 */
function MYMODULE_pathauto_alias_alter(&$alias, array &$context) {
  // Make sure we're dealing with nodes
  if (isset($context['module']) && $context['module'] == 'node') {
    // If this is a "my_content_type" content type
    if ($context['type'] == 'my_content_type') {
      $node = $context['data']['node'];

      // do stuff with $node, such as getting the taxonomy term
      // and loading the term.. then getting the children terms etc..

      // then assign a string value to $alias 
      $alias = pathauto_cleanstring($child_term->name) . '/' . pathauto_cleanstring($node->title); // for example...

      // Clean the alias
      $function = 'pathauto_clean_alias';
      if (function_exists($function)) {
        $alias = $function($alias);
      }

      // Now just make sure the alias generated is unique to this node.
      $function = 'pathauto_alias_uniquify';
      if (function_exists($function)) {
        $source = 'node/'.$node->nid;
        $lang = $node->language;

        $function($alias, $source, $lang);
      }
    }
  }
}
  • Thanks for the answer but I actually used Tokens with no need of any custom code. – pierostz Sep 5 '16 at 7:51
0

Thanks to @zhilevan and the link provided in his comment I got some hints.

In any case what actually worked is this:

[node:field-region-city-location:1:name]/[node:title]

So now I get correctly [child-term:name]/[node:name]

Thanks for the guidance everybody.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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