Add a [term:root-or-current] token, which returns the root term name of the selected term.

Public
</> CopyGet raw version
php
  1. /**
  2.  * Implements hook_token_info().
  3.  */
  4. function MY_MODULE_token_info() {
  5. $info = array();
  6.  
  7. //hook_token_info_item_types
  8.  
  9. // Fix a flaw in token module, as [term:root] is not set if the current term
  10. // itself is already the root. See also: https://www.drupal.org/node/2050421
  11. $info['tokens']['term']['root-or-current'] = array(
  12. 'type' => 'text',
  13. 'name' => t('Root or current term'),
  14. 'description' => t('Root of term or current term if it is already the root'),
  15. );
  16.  
  17. //hook_token_info_item_tokens
  18.  
  19. return $info;
  20. }
  21.  
  22. /**
  23.  * Implements hook_tokens().
  24.  */
  25. function MY_MODULE_tokens($type, $tokens, array $data = array(), array $options = array()) {
  26. $sanitize = !empty($options['sanitize']);
  27.  
  28. $replacements = array();
  29. if ($type == 'term' && !empty($data['term'])) {
  30. $term = $data['term'];
  31.  
  32. foreach ($tokens as $name => $original) {
  33. switch ($name) {
  34. case 'root-or-current':
  35. $parents = taxonomy_get_parents_all($term->tid);
  36. $root_term = end($parents);
  37. $replacements[$original] = $sanitize ? check_plain($root_term->name) : $root_term->name;
  38. break;
  39. }
  40. }
  41. }
  42.  
  43. return $replacements;
  44. }