Projet

Général

Profil

Paste
Télécharger (6,72 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / flag / flag.tokens.inc @ 018e218c

1
<?php
2

    
3
/**
4
 * @file
5
 * Flag module tokens support.
6
 */
7

    
8
/**
9
 * Implements of hook_token_info().
10
 */
11
function flag_token_info() {
12
  $types = array();
13
  $tokens = array();
14

    
15
  // Flag tokens.
16
  $types['flag'] = array(
17
    'name' => t('Flags'),
18
    'description' => t('Tokens related to flag data.'),
19
    'needs-data' => 'flag',
20
  );
21
  $tokens['flag']['name'] = array(
22
    'name' => t('Flag name'),
23
    'description' => t('The flag machine-readable name.'),
24
  );
25
  $tokens['flag']['title'] = array(
26
    'name' => t('Flag title'),
27
    'description' => t('The human-readable flag title.'),
28
  );
29

    
30
  // Flagging tokens.
31
  //
32
  // Attached fields are exposed as tokens via some contrib module, but we
33
  // need to expose other fields ourselves. Currently, 'date' is the only such
34
  // field we expose.
35
  $types['flagging'] = array(
36
    'name' => t('Flaggings'),
37
    'description' => t('Tokens related to flaggings.'),
38
    'needs-data' => 'flagging',
39
  );
40
  $tokens['flagging']['date'] = array(
41
    'name' => t('Flagging date'),
42
    'description' => t('The date an item was flagged.'),
43
    'type' => 'date',
44
  );
45

    
46
  // Flag action tokens.
47
  $types['flag-action'] = array(
48
    'name' => t('Flag actions'),
49
    'description' => t('Tokens available in response to a flag action being executed by a user.'),
50
    'needs-data' => 'flag-action',
51
  );
52
  $tokens['flag-action']['action'] = array(
53
    'name' => t('Flag action'),
54
    'description' => t('The flagging action taking place, either "flag" or "unflag".'),
55
  );
56
  $tokens['flag-action']['entity-url'] = array(
57
    'name' => t('Flag entity URL'),
58
    'description' => t('The URL of the entity being flagged.'),
59
  );
60
  $tokens['flag-action']['entity-title'] = array(
61
    'name' => t('Flag entity title'),
62
    'description' => t('The title of the entity being flagged.'),
63
  );
64
  $tokens['flag-action']['entity-type'] = array(
65
    'name' => t('Flag entity type'),
66
    'description' => t('The type of entity being flagged, such as <em>node</em> or <em>comment</em>.'),
67
  );
68
  $tokens['flag-action']['entity-id'] = array(
69
    'name' => t('Flag entity ID'),
70
    'description' => t('The ID of entity being flagged, such as a nid or cid.'),
71
  );
72
  $tokens['flag-action']['count'] = array(
73
    'name' => t('Flag count'),
74
    'description' => t('The current count total for this flag.'),
75
  );
76

    
77
  // Add tokens for the flag count available at the node/comment/user level.
78
  foreach (flag_get_types() as $flag_type) {
79
    $flags = flag_get_flags($flag_type);
80
    foreach ($flags as $flag) {
81
      $tokens[$flag_type]['flag-' . str_replace('_', '-', $flag->name) . '-count'] = array(
82
        'name' => t('@flag flag count', array('@flag' => $flag->get_title())),
83
        'description' => t('Total flag count for flag @flag', array('@flag' => $flag->get_title())),
84
      );
85
      $tokens[$flag_type]['flag-' . str_replace('_', '-', $flag->name) . '-link'] = array(
86
        'name' => t('@flag flag link', array('@flag' => $flag->get_title())),
87
        'description' => t('Flag/unflag link for @flag', array('@flag' => $flag->get_title())),
88
      );
89
    }
90
  }
91

    
92
  return array(
93
    'types' => $types,
94
    'tokens' => $tokens,
95
  );
96
}
97

    
98
/**
99
 * Implements hook_tokens().
100
 */
101
function flag_tokens($type, $tokens, array $data = array(), array $options = array()) {
102
  $replacements = array();
103
  $sanitize = !empty($options['sanitize']);
104
  $langcode = isset($options['language']) ? $options['language']->language : NULL;
105

    
106
  if ($type == 'flag' && !empty($data['flag'])) {
107
    $flag = $data['flag'];
108
    foreach ($tokens as $name => $original) {
109
      switch ($name) {
110
        case 'name':
111
          $replacements[$original] = $sanitize ? check_plain($flag->name) : $flag->name;
112
          break;
113
        case 'title':
114
          $replacements[$original] = $sanitize ? check_plain($flag->get_title()) : $flag->get_title();
115
          break;
116
      }
117
    }
118
  }
119
  elseif ($type == 'flagging' && !empty($data['flagging'])) {
120
    $flagging = $data['flagging'];
121
    foreach ($tokens as $name => $original) {
122
      switch ($name) {
123
        case 'date':
124
          $replacements[$original] = format_date($flagging->timestamp, 'medium', '', NULL, $langcode);
125
          break;
126
      }
127
    }
128
    if ($date_tokens = token_find_with_prefix($tokens, 'date')) {
129
      $replacements += token_generate('date', $date_tokens, array('date' => $flagging->timestamp), $options);
130
    }
131
  }
132
  elseif ($type == 'flag-action' && !empty($data['flag-action'])) {
133
    $action = $data['flag-action'];
134
    foreach ($tokens as $name => $original) {
135
      switch ($name) {
136
        case 'action':
137
          $replacements[$original] = $action->action;
138
          break;
139
        case 'entity-url':
140
          $replacements[$original] = $sanitize ? check_url($action->entity_url) : $action->entity_url;
141
          break;
142
        case 'entity-title':
143
          $replacements[$original] = $sanitize ? check_plain($action->entity_title) : $action->entity_title;
144
          break;
145
        case 'entity-type':
146
          $replacements[$original] = $action->entity_type;
147
          break;
148
        case 'entity-id':
149
          $replacements[$original] = $action->entity_id;
150
          break;
151
        case 'count':
152
          $replacements[$original] = $action->count;
153
          break;
154
      }
155
    }
156
  }
157

    
158
  if (isset($data[$type]) && in_array($type, flag_get_types())) {
159
    $flags = flag_get_flags($type);
160
    $object = $data[$type];
161
    foreach ($flags as $flag) {
162
      foreach ($tokens as $name => $original) {
163
        $flag_count_token = 'flag-' . str_replace('_', '-', $flag->name) . '-count';
164
        $flag_link_token = 'flag-' . str_replace('_', '-', $flag->name) . '-link';
165
        if ($name == $flag_count_token) {
166
          $replacements[$original] = $flag->get_count($flag->get_entity_id($object));
167
        }
168
        elseif ($name == $flag_link_token) {
169
          $replacements[$original] = flag_create_link($flag->name, $flag->get_entity_id($object));
170
        }
171
      }
172
    }
173
  }
174
  return $replacements;
175
}
176

    
177
/**
178
 * Returns HTML for a tokens browser.
179
 *
180
 * @param $variables
181
 *   An associative array containing:
182
 *   - types: An array naming the types of tokens to show.
183
 *   - global_types: Whether to show global tokens.
184
 */
185
function theme_flag_tokens_browser($variables) {
186
  $types = $variables['types'];
187
  $global_types = $variables['global_types'];
188

    
189
  if (module_exists('token')) {
190
    return theme('token_tree', array('token_types' => $types, 'global_types' => $global_types));
191
  }
192
  else {
193
    return '<p><em>' . t("Note: You don't have the <a href='@token-url'>Token</a> module installed, so the list of available tokens isn't shown here. You don't have to install <a href='@token-url'>Token</a> to be able to use tokens, but if you have it installed, and enabled, you'll be able to enjoy an interactive tokens browser.", array('@token-url' => 'http://drupal.org/project/token')) . '</em></p>';
194
  }
195
}