comparison sites/all/modules/ctools/plugins/contexts/token.inc @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ff03f76ab3fe
1 <?php
2
3 /**
4 * @file
5 * Provide a global context to allow for token support.
6 */
7
8 $plugin = array(
9 'title' => t('Token'),
10 'description' => t('A context that contains token replacements from token.module.'),
11 'context' => 'ctools_context_create_token', // func to create context
12 'context name' => 'token',
13 'keyword' => 'token',
14 'convert list' => 'ctools_context_token_convert_list',
15 'convert' => 'ctools_context_token_convert',
16 );
17
18 /**
19 * Create a context from manual configuration.
20 */
21 function ctools_context_create_token($empty, $data = NULL, $conf = FALSE) {
22 $context = new ctools_context('token');
23 $context->plugin = 'token';
24
25 return $context;
26 }
27
28 /**
29 * Implementation of hook_ctools_context_convert_list().
30 */
31 function ctools_context_token_convert_list() {
32 $tokens = token_info();
33 foreach ($tokens['types'] as $type => $type_info) {
34 if (empty($type_info['needs-data'])) {
35 $real_type = isset($type_info['type']) ? $type_info['type'] : $type;
36 foreach ($tokens['tokens'][$real_type] as $id => $info) {
37 $key = "$type:$id";
38 if (!isset($list[$key])) {
39 $list[$key] = $type_info['name'] . ': ' . $info['name'];
40 }
41 }
42 }
43 }
44
45 return $list;
46 }
47
48 /**
49 * Implementation of hook_ctools_context_converter_alter().
50 */
51 function ctools_context_token_convert($context, $token) {
52 $tokens = token_info();
53 list($type, $token) = explode(':', $token, 2);
54 $real_type = isset($tokens['types'][$type]['type']) ? $tokens['types'][$type]['type'] : $type;
55 if (isset($tokens['tokens'][$real_type][$token])) {
56 $values = token_generate($type, array($token => $token));
57 if (isset($values[$token])) {
58 return $values[$token];
59 }
60 }
61 }