1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222 | <?php
function token_example_menu() {
$items['examples/token'] = array(
'title' => 'Token example',
'description' => 'Test replacement tokens in real time.',
'page callback' => 'drupal_get_form',
'page arguments' => array('token_example_example_form'),
'access callback' => TRUE,
);
return $items;
}
function token_example_entity_info_alter(&$info) {
if (isset($info['taxonomy_term'])) {
$info['taxonomy_term']['token type'] = 'term';
}
if (isset($info['taxonomy_vocabulary'])) {
$info['taxonomy_vocabulary']['token type'] = 'vocabulary';
}
}
function token_example_example_form($form, &$form_state) {
$entities = entity_get_info();
$token_types = array();
foreach ($entities as $entity => $info) {
$object_callback = "_token_example_get_{$entity}";
if (function_exists($object_callback) && $objects = $object_callback()) {
$form[$entity] = array(
'#type' => 'select',
'#title' => $info['label'],
'#options' => array(0 => t('Not selected')) + $objects,
'#default_value' => isset($form_state['storage'][$entity]) ? $form_state['storage'][$entity] : 0,
'#access' => !empty($objects),
);
if ($form[$entity]['#access']) {
$token_types[$entity] = !empty($info['token type']) ? $info['token type'] : $entity;
}
}
}
$form['text'] = array(
'#type' => 'textarea',
'#title' => t('Enter your text here'),
'#default_value' => 'Hello [current-user:name]!'
);
if (!empty($form_state['storage']['text'])) {
$form['text']['#default_value'] = $form_state['storage']['text'];
$data = array();
foreach ($entities as $entity => $info) {
if (!empty($form_state['storage'][$entity])) {
$objects = entity_load($entity, array($form_state['storage'][$entity]));
if ($objects) {
$data[$token_types[$entity]] = reset($objects);
}
}
}
$form['text_tokenized'] = array(
'#type' => 'item',
'#title' => t('Result'),
'#markup' => token_replace($form_state['storage']['text'], $data),
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
if (module_exists('token')) {
$form['token_tree'] = array(
'#theme' => 'token_tree',
'#token_types' => $token_types,
);
}
else {
$form['token_tree'] = array(
'#markup' => '<p>' . t('Enable the <a href="@drupal-token">Token module</a> to view the available token browser.', array('@drupal-token' => 'http://drupal.org/project/token')) . '</p>',
);
}
return $form;
}
function token_example_example_form_submit($form, &$form_state) {
$form_state['storage'] = $form_state['values'];
$form_state['rebuild'] = TRUE;
}
function _token_example_get_node() {
if (!user_access('access content') && !user_access('bypass node access')) {
return array();
}
$node_query = db_select('node', 'n');
$node_query->fields('n', array('nid', 'title'));
$node_query->condition('n.status', NODE_PUBLISHED);
$node_query->orderBy('n.created', 'DESC');
$node_query->range(0, 10);
$node_query->addTag('node_access');
$nodes = $node_query->execute()->fetchAllKeyed();
$nodes = array_map('check_plain', $nodes);
return $nodes;
}
function _token_example_get_comment() {
if (!module_exists('comment') || (!user_access('access comments') && !user_access('administer comments'))) {
return array();
}
$comment_query = db_select('comment', 'c');
$comment_query->innerJoin('node', 'n', 'n.nid = c.nid');
$comment_query->fields('c', array('cid', 'subject'));
$comment_query->condition('n.status', NODE_PUBLISHED);
$comment_query->condition('c.status', COMMENT_PUBLISHED);
$comment_query->orderBy('c.created', 'DESC');
$comment_query->range(0, 10);
$comment_query->addTag('node_access');
$comments = $comment_query->execute()->fetchAllKeyed();
$comments = array_map('check_plain', $comments);
return $comments;
}
function _token_example_get_user() {
if (!user_access('access user profiles') &&
!user_access('administer users')) {
return array();
}
$account_query = db_select('users', 'u');
$account_query->fields('u', array('uid', 'name'));
$account_query->condition('u.uid', 0, '>');
$account_query->condition('u.status', 1);
$account_query->range(0, 10);
$accounts = $account_query->execute()->fetchAllKeyed();
$accounts = array_map('check_plain', $accounts);
return $accounts;
}
function _token_example_get_taxonomy_term() {
$term_query = db_select('taxonomy_term_data', 'ttd');
$term_query->fields('ttd', array('tid', 'name'));
$term_query->range(0, 10);
$term_query->addTag('term_access');
$terms = $term_query->execute()->fetchAllKeyed();
$terms = array_map('check_plain', $terms);
return $terms;
}
function _token_example_get_file() {
$file_query = db_select('file_managed', 'f');
$file_query->fields('f', array('fid', 'filename'));
$file_query->range(0, 10);
$files = $file_query->execute()->fetchAllKeyed();
$files = array_map('check_plain', $files);
return $files;
}
|