i18n_taxonomy.tokens.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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['localized-name'] = array(
  16. 'name' => t("Name in current language"),
  17. 'description' => t("The name of the taxonomy term in current language."),
  18. );
  19. $term['i18n-description'] = array(
  20. 'name' => t("Description (localized)"),
  21. 'description' => t("The optional description of the taxonomy term."),
  22. );
  23. // Taxonomy vocabulary related variables.
  24. $vocabulary['i18n-name'] = array(
  25. 'name' => t("Name (localized)"),
  26. 'description' => t("The name of the taxonomy vocabulary."),
  27. );
  28. $vocabulary['i18n-description'] = array(
  29. 'name' => t("Description (localized)"),
  30. 'description' => t("The optional description of the taxonomy vocabulary."),
  31. );
  32. // Chained tokens for taxonomies
  33. $term['i18n-vocabulary'] = array(
  34. 'name' => t("Vocabulary (localized)"),
  35. 'description' => t("The vocabulary the taxonomy term belongs to."),
  36. 'type' => 'vocabulary',
  37. );
  38. $term['i18n-parent'] = array(
  39. 'name' => t("Parent term (localized)"),
  40. 'description' => t("The parent term of the taxonomy term, if one exists."),
  41. 'type' => 'term',
  42. );
  43. return array(
  44. 'tokens' => array(
  45. 'term' => $term,
  46. 'vocabulary' => $vocabulary,
  47. ),
  48. );
  49. }
  50. /**
  51. * Implements hook_tokens().
  52. */
  53. function i18n_taxonomy_tokens($type, $tokens, array $data = array(), array $options = array()) {
  54. $replacements = array();
  55. $sanitize = !empty($options['sanitize']);
  56. $langcode = isset($options['language']) ? $options['language']->language : i18n_langcode();
  57. if ($type == 'term' && !empty($data['term'])) {
  58. $term = $data['term'];
  59. foreach ($tokens as $name => $original) {
  60. switch ($name) {
  61. case 'i18n-name':
  62. $name = i18n_taxonomy_term_name($term, $langcode);
  63. $replacements[$original] = $sanitize ? check_plain($name) : $name;
  64. break;
  65. case 'localized-name':
  66. $translated_term = i18n_taxonomy_term_get_translation($term, $langcode);
  67. $name = i18n_taxonomy_term_name($translated_term, $langcode);
  68. $replacements[$original] = $sanitize ? check_plain($name) : $name;
  69. break;
  70. case 'i18n-description':
  71. $replacements[$original] = i18n_string_text(array('taxonomy', 'term', $term->tid, 'description'), $term->description, array('langcode' => $langcode, 'format' => $term->format, 'sanitize' => $sanitize, 'cache' => TRUE));
  72. break;
  73. case 'i18n-vocabulary':
  74. $vocabulary = taxonomy_vocabulary_load($term->vid);
  75. $replacements[$original] = check_plain(i18n_taxonomy_vocabulary_name($vocabulary, $langcode));
  76. break;
  77. case 'i18n-parent':
  78. if ($parents = taxonomy_get_parents($term->tid)) {
  79. $parent = array_pop($parents);
  80. $replacements[$original] = check_plain(i18n_taxonomy_term_name($parent, $langcode));
  81. }
  82. break;
  83. }
  84. }
  85. }
  86. elseif ($type == 'vocabulary' && !empty($data['vocabulary'])) {
  87. $vocabulary = $data['vocabulary'];
  88. foreach ($tokens as $name => $original) {
  89. switch ($name) {
  90. case 'i18n-name':
  91. $name = i18n_taxonomy_vocabulary_name($vocabulary, $langcode);
  92. $replacements[$original] = $sanitize ? check_plain($name) : $name;
  93. break;
  94. case 'i18n-description':
  95. $description = i18n_object_langcode($vocabulary) ? $vocabulary->description : i18n_string(array('taxonomy', 'vocabulary', $vocabulary->vid, 'description'), $vocabulary->description, array('langcode' => $langcode));
  96. $replacements[$original] = $sanitize ? filter_xss($description) : $description;
  97. break;
  98. }
  99. }
  100. }
  101. return $replacements;
  102. }