summaryrefslogtreecommitdiff
path: root/kolab.org/www/drupal-7.26/sites/all/modules/ctools/plugins/contexts/token.inc
blob: 0445576916efdb3656e3e89cc0b581a9d610f59c (plain)
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
<?php

/**
 * @file
 *  Provide a global context to allow for token support.
 */

$plugin = array(
  'title' => t('Token'),
  'description' => t('A context that contains token replacements from token.module.'),
  'context' => 'ctools_context_create_token',  // func to create context
  'context name' => 'token',
  'keyword' => 'token',
  'convert list' => 'ctools_context_token_convert_list',
  'convert' => 'ctools_context_token_convert',
);

/**
 * Create a context from manual configuration.
 */
function ctools_context_create_token($empty, $data = NULL, $conf = FALSE) {
  $context = new ctools_context('token');
  $context->plugin = 'token';

  return $context;
}

/**
 * Implementation of hook_ctools_context_convert_list().
 */
function ctools_context_token_convert_list() {
  $tokens = token_info();
  foreach ($tokens['types'] as $type => $type_info) {
    if (empty($type_info['needs-data'])) {
      $real_type = isset($type_info['type']) ? $type_info['type'] : $type;
      foreach ($tokens['tokens'][$real_type] as $id => $info) {
        $key = "$type:$id";
        if (!isset($list[$key])) {
          $list[$key] = $type_info['name'] . ': ' . $info['name'];
        }
      }
    }
  }

  return $list;
}

/**
 * Implementation of hook_ctools_context_converter_alter().
 */
function ctools_context_token_convert($context, $token) {
  $tokens = token_info();
  list($type, $token) = explode(':', $token, 2);
  $parts = explode(':', $token, 2);
  $real_type = isset($tokens['types'][$type]['type']) ? $tokens['types'][$type]['type'] : $type;
  if (isset($tokens['tokens'][$real_type][$parts[0]])) {
    $values = token_generate($type, array($token => $token));
    if (isset($values[$token])) {
      return $values[$token];
    }
  }
}