Projet

Général

Profil

Paste
Télécharger (1,78 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / cas / cas.tokens.inc @ ba09eb79

1
<?php
2

    
3
/**
4
 * @file
5
 * Token module integration.
6
 */
7

    
8
/**
9
 * Implements hook_token_info().
10
 */
11
function cas_token_info() {
12
  $types['cas'] = array(
13
    'name' => t('CAS user'),
14
    'description' => t('Tokens related to a CAS user.'),
15
    'needs-data' => 'cas',
16
  );
17

    
18
  $cas['name'] = array(
19
    'name' => t('Name'),
20
    'description' => t('The CAS username.'),
21
  );
22

    
23
  return array(
24
    'types' => $types,
25
    'tokens' => array('cas' => $cas),
26
  );
27
}
28

    
29
/**
30
 * Implements hook_token_info_alter().
31
 */
32
function cas_token_info_alter(&$data) {
33
  $data['tokens']['user']['cas'] = array(
34
    'name' => t('CAS'),
35
    'description' => t("The account's primary CAS username."),
36
    'type' => 'cas',
37
  );
38
}
39

    
40
/**
41
 * Implements hook_tokens().
42
 */
43
function cas_tokens($type, $tokens, array $data = array(), array $options = array()) {
44
  $sanitize = !empty($options['sanitize']);
45
  $replacements = array();
46

    
47
  if ($type == 'cas' && !empty($data['cas'])) {
48
    $cas = $data['cas'];
49
    foreach ($tokens as $name => $original) {
50
      switch ($name) {
51
        case 'name':
52
          $replacements[$original] = $sanitize ? check_plain($cas) : $cas;
53
          break;
54
      }
55
    }
56
  }
57

    
58
  if ($type == 'user' && !empty($data['user'])) {
59
    $account = $data['user'];
60
    foreach ($tokens as $name => $original) {
61
      switch ($name) {
62
        case 'cas':
63
          // Provide [user:cas] token.
64
          if (!empty($account->cas_name)) {
65
            $replacements[$original] = $sanitize ? check_plain($account->cas_name) : $account->cas_name;
66
          }
67
          break;
68
      }
69
    }
70

    
71
    // Provide [user:cas:?] dynamic tokens.
72
    if ($cas_tokens = token_find_with_prefix($tokens, 'cas')) {
73
      $replacements += token_generate('cas', $cas_tokens, array('cas' => $account->cas_name), $options);
74
    }
75
  }
76

    
77
  return $replacements;
78
}