Project

General

Profile

Paste
Download (3.92 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / i18n / i18n_taxonomy / i18n_taxonomy.tokens.inc @ 01f36513

1
<?php
2

    
3
/**
4
 * @file
5
 * Builds placeholder replacement tokens for taxonomy terms and vocabularies.
6
 */
7

    
8
/**
9
 * Implements hook_token_info().
10
 */
11
function i18n_taxonomy_token_info() {
12

    
13
  // Taxonomy term related variables.
14
  $term['i18n-name'] = array(
15
    'name' => t("Name (localized)"),
16
    'description' => t("The name of the taxonomy term."),
17
  );
18
  $term['localized-name'] = array(
19
    'name' => t("Name in current language"),
20
    'description' => t("The name of the taxonomy term in current language."),
21
  );
22
  $term['i18n-description'] = array(
23
    'name' => t("Description (localized)"),
24
    'description' => t("The optional description of the taxonomy term."),
25
  );
26

    
27
  // Taxonomy vocabulary related variables.
28
  $vocabulary['i18n-name'] = array(
29
    'name' => t("Name (localized)"),
30
    'description' => t("The name of the taxonomy vocabulary."),
31
  );
32
  $vocabulary['i18n-description'] = array(
33
    'name' => t("Description (localized)"),
34
    'description' => t("The optional description of the taxonomy vocabulary."),
35
  );
36

    
37
  // Chained tokens for taxonomies
38
  $term['i18n-vocabulary'] = array(
39
    'name' => t("Vocabulary (localized)"),
40
    'description' => t("The vocabulary the taxonomy term belongs to."),
41
    'type' => 'vocabulary',
42
  );
43
  $term['i18n-parent'] = array(
44
    'name' => t("Parent term (localized)"),
45
    'description' => t("The parent term of the taxonomy term, if one exists."),
46
    'type' => 'term',
47
  );
48

    
49
  return array(
50
    'tokens' => array(
51
      'term' => $term,
52
      'vocabulary' => $vocabulary,
53
    ),
54
  );
55
}
56

    
57
/**
58
 * Implements hook_tokens().
59
 */
60
function i18n_taxonomy_tokens($type, $tokens, array $data = array(), array $options = array()) {
61
  $replacements = array();
62
  $sanitize = !empty($options['sanitize']);
63
  $langcode = isset($options['language']) ? $options['language']->language : i18n_langcode();
64

    
65
  if ($type == 'term' && !empty($data['term'])) {
66
    $term = $data['term'];
67

    
68
    foreach ($tokens as $name => $original) {
69
      switch ($name) {
70

    
71
        case 'i18n-name':
72
          $name = i18n_taxonomy_term_name($term, $langcode);
73
          $replacements[$original] = $sanitize ? check_plain($name) : $name;
74
          break;
75

    
76
        case 'localized-name':
77
          $translated_term = i18n_taxonomy_term_get_translation($term, $langcode);
78
          $name = i18n_taxonomy_term_name($translated_term, $langcode);
79
          $replacements[$original] = $sanitize ? check_plain($name) : $name;
80
          break;
81

    
82
        case 'i18n-description':
83
          $replacements[$original] = i18n_string_text(array('taxonomy', 'term', $term->tid, 'description'), $term->description, array('langcode' => $langcode, 'format' => $term->format, 'sanitize' => $sanitize, 'cache' => TRUE));
84
          break;
85

    
86
        case 'i18n-vocabulary':
87
          $vocabulary = taxonomy_vocabulary_load($term->vid);
88
          $replacements[$original] = check_plain(i18n_taxonomy_vocabulary_name($vocabulary, $langcode));
89
          break;
90

    
91
        case 'i18n-parent':
92
          if ($parents = taxonomy_get_parents($term->tid)) {
93
            $parent = array_pop($parents);
94
            $replacements[$original] = check_plain(i18n_taxonomy_term_name($parent, $langcode));
95
          }
96
          break;
97
      }
98
    }
99
  }
100

    
101
  elseif ($type == 'vocabulary' && !empty($data['vocabulary'])) {
102
    $vocabulary = $data['vocabulary'];
103

    
104
    foreach ($tokens as $name => $original) {
105
      switch ($name) {
106

    
107
        case 'i18n-name':
108
          $name = i18n_taxonomy_vocabulary_name($vocabulary, $langcode);
109
          $replacements[$original] = $sanitize ? check_plain($name) : $name;
110
          break;
111

    
112
        case 'i18n-description':
113
          $description = i18n_object_langcode($vocabulary) ? $vocabulary->description : i18n_string(array('taxonomy', 'vocabulary', $vocabulary->vid, 'description'), $vocabulary->description, array('langcode' => $langcode));
114
          $replacements[$original] = $sanitize ? filter_xss($description) : $description;
115
          break;
116
      }
117
    }
118
  }
119

    
120
  return $replacements;
121
}