I don’t remember in which sandbox project I found this, but this is the perfect solution.
taxonomy_path_token.info
name = Taxonomy Path Token description = Taxonomy path token creates a path of parent taxonomy terms of a node package = Token core = 7.x dependencies[] = token
taxonomy_path_token.module
<?php function taxonomy_path_token_tokens($type, $tokens, array $data = array(), array $options = array()) { $replacements = array(); if (!empty($tokens['taxonomy_path']) && !empty($data['node'])) { if(!empty($options['sanitize'])) { $sanitize = $options['sanitize']; } else { $sanitize = FALSE; } $node = $data['node']; $replacements[$tokens['taxonomy_path']] = $sanitize ? check_plain(taxonomy_path_token_get_parents($node)) : taxonomy_path_token_get_parents($node); } if ($type == 'array' && !empty($data['array'])) { $array = $data['array']; foreach ($tokens as $name => $original) { switch ($name) { case 'join-path-except-first': module_load_include('inc', 'pathauto'); $values = array(); foreach (element_children($array) as $key) { $value = is_array($array[$key]) ? render($array[$key]) : (string) $array[$key]; $value = pathauto_cleanstring($value); $values[] = $value; } array_shift($values); $replacements[$original] = implode('/', $values); break; } } } return $replacements; } function taxonomy_path_token_token_info() { $info['tokens']['node']['taxonomy_path'] = array( 'name' => t('taxonomy_path'), 'description' => t('Custom taxonomy_path token.'), ); $info['tokens']['array']['join-path-except-first'] = array( 'name' => t('Joined path'), 'description' => t('The array values each cleaned by Pathauto and then joined with the slash into a string that resembles an URL.'), ); return $info; } function taxonomy_path_token_get_parents($node) { module_load_include('inc','pathauto','pathauto'); if(!empty($node->field_tags)){ $tid = current($node->field_tags); $tid = $tid[0]['tid']; } else{ return ''; } $parents = taxonomy_get_parents_all($tid); $paths = array(); foreach ($parents as $parent) { $paths[] = pathauto_cleanstring($parent->name); } $paths = array_reverse($paths); array_shift($paths); $pathauto = implode('/', $paths); return $pathauto; }
Then add this "[node: taxonomy_path] / [node: title]" to your pathauto templates.
George Mastro
source share