Projet

Général

Profil

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

root / drupal7 / sites / all / modules / cas_attributes / cas_attributes.tokens.inc @ 87dbc3bf

1
<?php
2

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

    
8
/**
9
 * Implements hook_token_info_alter().
10
 */
11
function cas_attributes_token_info_alter(&$data) {
12
  if (function_exists('cas_phpcas_attributes')) {
13
    $data['tokens']['cas']['attribute'] = array(
14
      'name' => t('Attribute'),
15
      'description' => t('A CAS attribute of the user. (Only available if user is logged into CAS). Always stored as an array token (thus supporting multivalue attributes); therefore be sure to remember to specify which array member you want (for instance, to get the first value: [cas:attribute:?:first]).  <a href="@url">Available tokens</a>.', array('@url' => url('admin/config/people/cas/attributes/cas'))),
16
      'dynamic' => TRUE,
17
    );
18
  }
19
}
20

    
21
/**
22
 * Implements hook_tokens().
23
 */
24
function cas_attributes_tokens($type, $tokens, array $data = array(), array $options = array()) {
25
  $sanitize = !empty($options['sanitize']);
26
  $replacements = array();
27

    
28
  if ($type == 'cas' && !empty($data['cas'])) {
29
    $cas = $data['cas'];
30

    
31
    // Provide [cas:attribute:?] dynamic tokens.
32
    if ($attribute_tokens = token_find_with_prefix($tokens, 'attribute')) {
33
      $attribute = array_change_key_case(cas_phpcas_attributes($cas));
34
      foreach ($attribute_tokens as $name => $original) {
35
        // If there are no options specified have it return all values.
36
        if (strpos($name, ':') === FALSE) {
37
          $name .= ':join';
38
        }
39

    
40
        // Break out the token into attributes and the options for them.
41
        list($name, $token) = explode(':', $name, 2);
42

    
43
        $name = drupal_strtolower($name);
44
        if (isset($attribute[$name])) {
45
          $value = $attribute[$name];
46
          if (!is_array($value)) {
47
            $value = array($value);
48
          }
49
          $replacements += token_generate('array', array($token => $original), array('array' => $value), $options);
50
        }
51
        elseif ($name == '?') {
52
          $keys = array_keys($attribute);
53
          if ($sanitize) {
54
            $keys = array_map('check_plain', $keys);
55
          }
56
          $replacements[$original] = t('Available attributes: %keys', array('%keys' => implode(', ', $keys)));
57
        }
58
      }
59
    }
60
  }
61

    
62
  return $replacements;
63
}