1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * User page callbacks for the token module. |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * For a given context, builds a formatted list of tokens and descriptions |
---|
10 | * of their replacement values. |
---|
11 | * |
---|
12 | * @param types |
---|
13 | * The token types to display documentation for. Can be either a single |
---|
14 | * string or an array of token types. Defaults to 'all'. |
---|
15 | * @param prefix |
---|
16 | * The prefix your module will use when parsing tokens. Defaults to '[' |
---|
17 | * @param suffix |
---|
18 | * The suffix your module will use when parsing tokens. Defaults to ']' |
---|
19 | * @return An HTML table containing the formatting docs. |
---|
20 | * |
---|
21 | * @ingroup themeable |
---|
22 | */ |
---|
23 | function theme_token_help($types = 'all', $prefix = TOKEN_PREFIX, $suffix = TOKEN_SUFFIX) { |
---|
24 | token_include(); |
---|
25 | $full_list = token_get_list($types); |
---|
26 | |
---|
27 | $headers = array(t('Token'), t('Replacement value')); |
---|
28 | $rows = array(); |
---|
29 | foreach ($full_list as $key => $category) { |
---|
30 | $rows[] = array(array('data' => t('@type tokens', array('@type' => drupal_ucfirst($key))), 'class' => 'region', 'colspan' => 2)); |
---|
31 | foreach ($category as $token => $description) { |
---|
32 | $row = array(); |
---|
33 | $row[] = $prefix . $token . $suffix; |
---|
34 | $row[] = $description; |
---|
35 | $rows[] = $row; |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | $output = theme('table', $headers, $rows, array('class' => 'description')); |
---|
40 | return $output; |
---|
41 | } |
---|
42 | |
---|
43 | /** |
---|
44 | * Provide a 'tree' display of nested tokens. |
---|
45 | * |
---|
46 | * @ingroup themeable |
---|
47 | */ |
---|
48 | function theme_token_tree($token_types = array(), $global_types = TRUE, $click_insert = TRUE) { |
---|
49 | if ($token_types == 'all' || !is_array($token_types) || in_array('all', $token_types)) { |
---|
50 | $token_types = array('all'); |
---|
51 | } |
---|
52 | elseif ($global_types) { |
---|
53 | $token_types[] = 'global'; |
---|
54 | } |
---|
55 | else { |
---|
56 | $global_key = array_search('global', $token_types); |
---|
57 | if ($global_key !== FALSE) { |
---|
58 | unset($token_types[$global_key]); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | // Check for token type validity and sort. |
---|
63 | $token_types = array_unique($token_types); |
---|
64 | $info = token_get_list($token_types); |
---|
65 | //$token_types = array_intersect($token_types, array_keys($info)); |
---|
66 | $token_types = array_keys($info); |
---|
67 | sort($token_types); |
---|
68 | |
---|
69 | $header = array( |
---|
70 | t('Token'), |
---|
71 | t('Description'), |
---|
72 | ); |
---|
73 | $rows = array(); |
---|
74 | |
---|
75 | foreach ($token_types as $type) { |
---|
76 | $parent = NULL; |
---|
77 | |
---|
78 | if (count($token_types) > 1) { |
---|
79 | $rows[] = _token_token_tree_format_row($type, array(), TRUE); |
---|
80 | $parent = $type; |
---|
81 | } |
---|
82 | |
---|
83 | foreach ($info[$type] as $token => $description) { |
---|
84 | $rows[] = _token_token_tree_format_row("[$token]", array('description' => $description, 'parent' => $parent)); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | if (count($rows)) { |
---|
89 | drupal_add_js(drupal_get_path('module', 'token') . '/jquery.treeTable.js'); |
---|
90 | drupal_add_css(drupal_get_path('module', 'token') . '/jquery.treeTable.css'); |
---|
91 | drupal_add_js(drupal_get_path('module', 'token') . '/token.js'); |
---|
92 | drupal_add_css(drupal_get_path('module', 'token') . '/token.css'); |
---|
93 | } |
---|
94 | else { |
---|
95 | $rows[] = array(array( |
---|
96 | 'data' => t('No tokens available.'), |
---|
97 | 'colspan' => 2, |
---|
98 | )); |
---|
99 | } |
---|
100 | |
---|
101 | $table_options = array( |
---|
102 | 'attributes' => array('class' => 'token-tree'), |
---|
103 | 'caption' => '', |
---|
104 | ); |
---|
105 | if ($click_insert) { |
---|
106 | $table_options['caption'] = t('Click a token to insert it into the field you\'ve last clicked.'); |
---|
107 | $table_options['attributes']['class'] .= ' token-click-insert'; |
---|
108 | } |
---|
109 | return theme('table', $header, $rows, $table_options['attributes'], $table_options['caption']); |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * Build a row in the token tree. |
---|
114 | */ |
---|
115 | function _token_token_tree_format_row($token, $token_info = array(), $is_group = FALSE) { |
---|
116 | $row = array( |
---|
117 | 'id' => _token_clean_css_identifier($token), |
---|
118 | 'class' => array(), |
---|
119 | 'data' => array( |
---|
120 | 'token' => '', |
---|
121 | 'description' => !empty($token_info['description']) ? $token_info['description'] : '', |
---|
122 | ), |
---|
123 | ); |
---|
124 | |
---|
125 | if ($is_group) { |
---|
126 | // This is a token type/group. |
---|
127 | $row['data']['token'] = drupal_ucfirst($token); |
---|
128 | $row['class'][] = 'token-group'; |
---|
129 | $row['id'] .= '-group'; |
---|
130 | } |
---|
131 | else { |
---|
132 | // This is a token. |
---|
133 | $row['data']['token'] = array( |
---|
134 | 'data' => $token, |
---|
135 | 'class' => 'token-key', |
---|
136 | ); |
---|
137 | if (!empty($token_info['parent'])) { |
---|
138 | $row['class'][] = 'child-of-' . _token_clean_css_identifier($token_info['parent']) . '-group'; |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | $row['class'] = implode(' ', $row['class']); |
---|
143 | |
---|
144 | return $row; |
---|
145 | } |
---|
146 | |
---|
147 | function _token_clean_css_identifier($id) { |
---|
148 | return 'token-' . rtrim(str_replace(array('][', '_', ' ', ':'), '-', trim($id, '[]')), '-'); |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * Menu callback; prints the available tokens and values for an object. |
---|
153 | */ |
---|
154 | function token_devel_token_object($entity, $object) { |
---|
155 | $tokens = token_get_values($entity, $object); |
---|
156 | $tokens = array_combine($tokens->tokens, $tokens->values); |
---|
157 | return kdevel_print_object($tokens); |
---|
158 | } |
---|