e773657a9402bda3bd398e618c1a67bd04b051a8
[project/cck.git] / includes / content.token.inc
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Implementation of hook_content_build_modes
7 * (on behalf of token.module)
8 */
9 function token_content_build_modes() {
10 return array(
11 'token' => array(
12 'title' => t('Token'),
13 'build modes' => array(
14 'token' => array(
15 'title' => t('Token'),
16 'views style' => FALSE,
17 ),
18 ),
19 ),
20 );
21 }
22
23 // Two helper functions that generate appropriate tokens for CCK-added fields.
24 function content_token_values($type, $object = NULL) {
25 $tokens = array();
26 if ($type == 'node') {
27 $node = $object;
28 // Prevent against invalid 'nodes' built by broken 3rd party code.
29 if (isset($node->type)) {
30 $type = content_types($node->type);
31 $node->build_mode = 'token';
32 $node->content = array();
33 content_view($node);
34 // The formatted values will only be known after the content has been rendered.
35 drupal_render($node->content);
36 content_alter($node);
37
38 $field_types = _content_field_types();
39 foreach ($type['fields'] as $field) {
40 $items = $node->{$field['field_name']};
41 $function = $field_types[$field['type']]['module'] . '_token_values';
42 if (!empty($items) && function_exists($function)) {
43 $token_values = $function('field', $items);
44 foreach ($token_values as $token => $value) {
45 $tokens[$field['field_name'] .'-'. $token] = $value;
46 }
47 }
48 }
49 }
50 }
51 return $tokens;
52 }
53
54 function content_token_list($type = 'all') {
55 if ($type == 'node' || $type == 'all') {
56 $list = array();
57 $field_types = _content_field_types();
58
59 foreach (content_fields() as $field) {
60 $sub_list = array();
61 $function = $field_types[$field['type']]['module'] . '_token_list';
62 if (function_exists($function)) {
63 $sub_list = $function('field');
64 foreach ($sub_list as $category => $token_list) {
65 foreach ($token_list as $token => $description) {
66 $list['CCK '. $category][$field['field_name'] .'-'. $token] = $description;
67 }
68 }
69 }
70 }
71
72 return $list;
73 }
74 }
75
76 if (module_exists('nodereference')) {
77 function nodereference_token_list($type = 'all') {
78 if ($type == 'field' || $type == 'all') {
79 $tokens = array();
80
81 $tokens['node reference']['nid'] = t('Referenced node ID');
82 $tokens['node reference']['title'] = t('Referenced node title');
83 $tokens['node reference']['title-raw'] = t('Referenced node unfiltered title. WARNING - raw user input.');
84 $tokens['node reference']['link'] = t("Formatted html link to the referenced node.");
85 $tokens['node reference']['path'] = t("Relative path alias to the referenced node.");
86 $tokens['node reference']['url'] = t("Absolute path alias to the referenced node.");
87
88 return $tokens;
89 }
90 }
91
92 function nodereference_token_values($type, $object = NULL) {
93 if ($type == 'field') {
94 $item = $object[0];
95
96 $title = is_numeric($item['nid']) ? _nodereference_titles($item['nid']) : '';
97 $tokens['nid'] = $item['nid'];
98 $tokens['title'] = $title ? check_plain($title) : '';
99 $tokens['title-raw'] = $title;
100 $tokens['link'] = isset($item['view']) ? $item['view'] : '';
101 $tokens['path'] = is_numeric($item['nid']) ? url('node/' . $item['nid']) : '';
102 $tokens['url'] = is_numeric($item['nid']) ? url('node/' . $item['nid'], array('absolute' => TRUE)) : '';
103
104 return $tokens;
105 }
106 }
107 }
108
109 if (module_exists('number')) {
110 function number_token_list($type = 'all') {
111 if ($type == 'field' || $type == 'all') {
112 $tokens = array();
113
114 $tokens['number']['raw'] = t('Raw number value');
115 $tokens['number']['formatted'] = t('Formatted number value');
116
117 return $tokens;
118 }
119 }
120
121 function number_token_values($type, $object = NULL) {
122 if ($type == 'field') {
123 $item = $object[0];
124
125 $tokens['raw'] = $item['value'];
126 $tokens['formatted'] = isset($item['view']) ? $item['view'] : '';
127
128 return $tokens;
129 }
130 }
131 }
132
133 if (module_exists('text')) {
134 function text_token_list($type = 'all') {
135 if ($type == 'field' || $type == 'all') {
136 $tokens = array();
137
138 $tokens['text']['raw'] = t('Raw, unfiltered text');
139 $tokens['text']['formatted'] = t('Formatted and filtered text');
140
141 return $tokens;
142 }
143 }
144
145 function text_token_values($type, $object = NULL) {
146 if ($type == 'field') {
147 $item = $object[0];
148
149 $tokens['raw'] = $item['value'];
150 $tokens['formatted'] = isset($item['view']) ? $item['view'] : '';
151 return $tokens;
152 }
153 }
154 }
155
156 if (module_exists('userreference')) {
157 function userreference_token_list($type = 'all') {
158 if ($type == 'field' || $type == 'all') {
159 $tokens = array();
160
161 $tokens['user reference']['uid'] = t('Referenced user ID');
162 $tokens['user reference']['name'] = t('Referenced user name');
163 $tokens['user reference']['link'] = t('Formatted HTML link to referenced user');
164 $tokens['user reference']['path'] = t("Relative path alias to the referenced user.");
165 $tokens['user reference']['url'] = t("Absolute path alias to the referenced user.");
166
167 return $tokens;
168 }
169 }
170
171 function userreference_token_values($type, $object = NULL) {
172 if ($type == 'field') {
173 $item = $object[0];
174
175 $tokens['uid'] = $item['uid'];
176 $tokens['name'] = isset($item['view']) ? strip_tags($item['view']) : '';
177 $tokens['link'] = isset($item['view']) ? $item['view'] : '';
178 $tokens['path'] = is_numeric($item['uid']) ? url('user/' . $item['uid']) : '';
179 $tokens['url'] = is_numeric($item['uid']) ? url('user/' . $item['uid'], array('absolute' => TRUE)) : '';
180
181 return $tokens;
182 }
183 }
184 }