/[drupal]/drupal/modules/node/node.tokens.inc
ViewVC logotype

Contents of /drupal/modules/node/node.tokens.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Wed Oct 21 19:13:08 2009 UTC (5 weeks, 4 days ago) by dries
Branch: MAIN
Changes since 1.4: +2 -2 lines
File MIME type: text/x-php
- Patch #609090 by te-brian: fixed invalid call to node_get_types()
1 <?php
2 // $Id: node.tokens.inc,v 1.4 2009/10/11 03:07:18 webchick Exp $
3
4 /**
5 * @file
6 * Builds placeholder replacement tokens for node-related data.
7 */
8
9
10
11 /**
12 * Implement hook_token_info().
13 */
14 function node_token_info() {
15 $type = array(
16 'name' => t('Nodes'),
17 'description' => t('Tokens related to individual nodes.'),
18 'needs-data' => 'node',
19 );
20
21 // Core tokens for nodes.
22 $node['nid'] = array(
23 'name' => t("Node ID"),
24 'description' => t("The unique ID of the node."),
25 );
26 $node['vid'] = array(
27 'name' => t("Revision ID"),
28 'description' => t("The unique ID of the node's latest revision."),
29 );
30 $node['tnid'] = array(
31 'name' => t("Translation set ID"),
32 'description' => t("The unique ID of the original-language version of this node, if one exists."),
33 );
34 $node['uid'] = array(
35 'name' => t("User ID"),
36 'description' => t("The unique ID of the user who posted the node."),
37 );
38 $node['type'] = array(
39 'name' => t("Content type"),
40 'description' => t("The type of the node."),
41 );
42 $node['type-name'] = array(
43 'name' => t("Content type name"),
44 'description' => t("The human-readable name of the node type."),
45 );
46 $node['title'] = array(
47 'name' => t("Title"),
48 'description' => t("The title of the node."),
49 );
50 $node['body'] = array(
51 'name' => t("Body"),
52 'description' => t("The main body text of the node."),
53 );
54 $node['summary'] = array(
55 'name' => t("Summary"),
56 'description' => t("The summary of the node's main body text."),
57 );
58 $node['language'] = array(
59 'name' => t("Language"),
60 'description' => t("The language the node is written in."),
61 );
62 $node['url'] = array(
63 'name' => t("URL"),
64 'description' => t("The URL of the node."),
65 );
66 $node['edit-url'] = array(
67 'name' => t("Edit URL"),
68 'description' => t("The URL of the node's edit page."),
69 );
70
71 // Chained tokens for nodes.
72 $node['created'] = array(
73 'name' => t("Date created"),
74 'description' => t("The date the node was posted."),
75 'type' => 'date',
76 );
77 $node['changed'] = array(
78 'name' => t("Date changed"),
79 'description' => t("The date the node was most recently updated."),
80 'type' => 'date',
81 );
82 $node['author'] = array(
83 'name' => t("Author"),
84 'description' => t("The author of the node."),
85 'type' => 'user',
86 );
87
88 return array(
89 'types' => array('node' => $type),
90 'tokens' => array('node' => $node),
91 );
92 }
93
94 /**
95 * Implement hook_tokens().
96 */
97 function node_tokens($type, $tokens, array $data = array(), array $options = array()) {
98 $url_options = array('absolute' => TRUE);
99 if (isset($options['language'])) {
100 $url_options['language'] = $options['language'];
101 $language_code = $options['language']->language;
102 }
103 else {
104 $language_code = NULL;
105 }
106 $sanitize = !empty($options['sanitize']);
107
108 $replacements = array();
109
110 if ($type == 'node' && !empty($data['node'])) {
111 $node = $data['node'];
112
113 foreach ($tokens as $name => $original) {
114 switch ($name) {
115 // Simple key values on the node.
116 case 'nid':
117 $replacements[$original] = $node->nid;
118 break;
119
120 case 'vid':
121 $replacements[$original] = $node->vid;
122 break;
123
124 case 'tnid':
125 $replacements[$original] = $node->tnid;
126 break;
127
128 case 'uid':
129 $replacements[$original] = $node->uid;
130 break;
131
132 case 'name':
133 $replacements[$original] = $sanitize ? check_plain($node->name) : $node->name;
134 break;
135
136 case 'title':
137 $replacements[$original] = $sanitize ? check_plain($node->title[FIELD_LANGUAGE_NONE][0]['value']) : $node->title[FIELD_LANGUAGE_NONE][0]['value'];
138 break;
139
140 case 'body':
141 if (!empty($node->body)) {
142 $replacements[$original] = $sanitize ? $node->body[0]['safe'] : $node->body[0]['value'];
143 }
144 break;
145
146 case 'summary':
147 if (!empty($node->body)) {
148 $replacements[$original] = $sanitize ? $node->body[0]['safe_summary'] : $node->body[0]['summary'];
149 }
150 break;
151
152 case 'type':
153 $replacements[$original] = $sanitize ? check_plain($node->type) : $node->type;
154 break;
155
156 case 'type-name':
157 $type_name = node_type_get_name($node);
158 $replacements[$original] = $sanitize ? check_plain($type_name) : $type_name;
159 break;
160
161 case 'language':
162 $replacements[$original] = $sanitize ? check_plain($node->language) : $node->language;
163 break;
164
165 case 'url':
166 $replacements[$original] = url('node/' . $node->nid, array('absolute' => TRUE));
167 break;
168
169 case 'edit-url':
170 $replacements[$original] = url('node/' . $node->nid . '/edit', array('absolute' => TRUE));
171 break;
172
173 // Default values for the chained tokens handled below.
174 case 'author':
175 $name = ($node->uid == 0) ? variable_get('anonymous', t('Anonymous')) : $node->name;
176 $replacements[$original] = $sanitize ? filter_xss($name) : $name;
177 break;
178
179 case 'created':
180 $replacements[$original] = format_date($node->created, 'medium', '', NULL, $language_code);
181 break;
182
183 case 'changed':
184 $replacements[$original] = format_date($node->changed, 'medium', '', NULL, $language_code);
185 break;
186 }
187 }
188
189 if ($author_tokens = token_find_with_prefix($tokens, 'author')) {
190 $author = user_load($node->uid);
191 $replacements += token_generate('user', $author_tokens, array('user' => $author), $options);
192 }
193
194 if ($created_tokens = token_find_with_prefix($tokens, 'created')) {
195 $replacements += token_generate('date', $created_tokens, array('date' => $node->created), $options);
196 }
197
198 if ($changed_tokens = token_find_with_prefix($tokens, 'changed')) {
199 $replacements += token_generate('date', $changed_tokens, array('date' => $node->changed), $options);
200 }
201 }
202
203 return $replacements;
204 }

  ViewVC Help
Powered by ViewVC 1.1.2