Projet

Général

Profil

Paste
Télécharger (2,25 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / plugins / contexts / token.inc @ 6e3ce7c2

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
 * @param $empty
22
 *   Unused.
23
 * @param $data
24
 *   Unused.
25
 * @param $conf
26
 *   Unused.
27
 *
28
 * @return ctools_context
29
 *   A context of type token, with the plugin set appropriately.
30
 */
31
function ctools_context_create_token($empty, $data = NULL, $conf = FALSE) {
32
  $context = new ctools_context('token');
33
  $context->plugin = 'token';
34

    
35
  return $context;
36
}
37

    
38
/**
39
 * Implementation of hook_ctools_context_convert_list().
40
 *
41
 * @return array|null
42
 *   An array of token type information, keyed by 'type:id', or NULL if
43
 *   none found.
44
 */
45
function ctools_context_token_convert_list() {
46
  $tokens = token_info();
47
  // Initialise $list here?
48
  foreach ($tokens['types'] as $type => $type_info) {
49
    if (empty($type_info['needs-data'])) {
50
      $real_type = isset($type_info['type']) ? $type_info['type'] : $type;
51
      foreach ($tokens['tokens'][$real_type] as $id => $info) {
52
        $key = "$type:$id";
53
        if (!isset($list[$key])) {
54
          $list[$key] = $type_info['name'] . ': ' . $info['name'];
55
        }
56
      }
57
    }
58
  }
59

    
60
  return $list;
61
}
62

    
63
/**
64
 * Token conversion function: look up the token and return it's value.
65
 *
66
 * @param $context
67
 *   Unused.
68
 * @param string $token
69
 *   The name of the token.
70
 *
71
 * @return array|null
72
 *   The token value, or NULL if not found.
73
 *
74
 * @see ctools_context_convert_context()
75
 */
76
function ctools_context_token_convert($context, $token) {
77
  $tokens = token_info();
78
  list($type, $token) = explode(':', $token, 2);
79
  $parts = explode(':', $token, 2);
80
  $real_type = isset($tokens['types'][$type]['type']) ? $tokens['types'][$type]['type'] : $type;
81
  if (isset($tokens['tokens'][$real_type][$parts[0]])) {
82
    $values = token_generate($type, array($token => $token));
83
    if (isset($values[$token])) {
84
      return $values[$token];
85
    }
86
  }
87
  return NULL;
88
}