i18n_taxonomy.tokens.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * @file
  4. * Builds placeholder replacement tokens for taxonomy terms and vocabularies.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function i18n_taxonomy_token_info() {
  10. // Taxonomy term related variables.
  11. $term['i18n-name'] = array(
  12. 'name' => t("Name (localized)"),
  13. 'description' => t("The name of the taxonomy term."),
  14. );
  15. $term['i18n-description'] = array(
  16. 'name' => t("Description (localized)"),
  17. 'description' => t("The optional description of the taxonomy term."),
  18. );
  19. // Taxonomy vocabulary related variables.
  20. $vocabulary['i18n-name'] = array(
  21. 'name' => t("Name (localized)"),
  22. 'description' => t("The name of the taxonomy vocabulary."),
  23. );
  24. $vocabulary['i18n-description'] = array(
  25. 'name' => t("Description (localized)"),
  26. 'description' => t("The optional description of the taxonomy vocabulary."),
  27. );
  28. // Chained tokens for taxonomies
  29. $term['i18n-vocabulary'] = array(
  30. 'name' => t("Vocabulary (localized)"),
  31. 'description' => t("The vocabulary the taxonomy term belongs to."),
  32. 'type' => 'vocabulary',
  33. );
  34. $term['i18n-parent'] = array(
  35. 'name' => t("Parent term (localized)"),
  36. 'description' => t("The parent term of the taxonomy term, if one exists."),
  37. 'type' => 'term',
  38. );
  39. return array(
  40. 'tokens' => array(
  41. 'term' => $term,
  42. 'vocabulary' => $vocabulary,
  43. ),
  44. );
  45. }
  46. /**
  47. * Implements hook_tokens().
  48. */
  49. function i18n_taxonomy_tokens($type, $tokens, array $data = array(), array $options = array()) {
  50. $replacements = array();
  51. $sanitize = !empty($options['sanitize']);
  52. $langcode = isset($options['language']) ? $options['language']->language : i18n_langcode();
  53. if ($type == 'term' && !empty($data['term'])) {
  54. $term = $data['term'];
  55. foreach ($tokens as $name => $original) {
  56. switch ($name) {
  57. case 'i18n-name':
  58. $name = i18n_taxonomy_term_name($term, $langcode);
  59. $replacements[$original] = $sanitize ? check_plain($name) : $name;
  60. break;
  61. case 'i18n-description':
  62. $replacements[$original] = i18n_string_text(array('taxonomy', 'term', $term->tid, 'description'), $term->description, array('langcode' => $langcode, 'format' => $term->format, 'sanitize' => $sanitize, 'cache' => TRUE));
  63. break;
  64. case 'i18n-vocabulary':
  65. $vocabulary = taxonomy_vocabulary_load($term->vid);
  66. $replacements[$original] = check_plain(i18n_taxonomy_vocabulary_name($vocabulary, $langcode));
  67. break;
  68. case 'i18n-parent':
  69. if ($parents = taxonomy_get_parents($term->tid)) {
  70. $parent = array_pop($parents);
  71. $replacements[$original] = check_plain(i18n_taxonomy_term_name($parent, $langcode));
  72. }
  73. break;
  74. }
  75. }
  76. }
  77. elseif ($type == 'vocabulary' && !empty($data['vocabulary'])) {
  78. $vocabulary = $data['vocabulary'];
  79. foreach ($tokens as $name => $original) {
  80. switch ($name) {
  81. case 'i18n-name':
  82. $name = i18n_taxonomy_vocabulary_name($vocabulary, $langcode);
  83. $replacements[$original] = $sanitize ? check_plain($name) : $name;
  84. break;
  85. case 'i18n-description':
  86. $description = i18n_object_langcode($vocabulary) ? $vocabulary->description : i18n_string(array('taxonomy', 'vocabulary', $vocabulary->vid, 'description'), $vocabulary->description, array('langcode' => $langcode));
  87. $replacements[$original] = $sanitize ? filter_xss($description) : $description;
  88. break;
  89. }
  90. }
  91. }
  92. return $replacements;
  93. }