Projet

Général

Profil

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

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

1
<?php
2

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

    
8
/**
9
 * Implements of hook_token_info().
10
 *
11
 * The tokens we provide on generic entities require token module.
12
 */
13
function flag_token_info() {
14
  $types = array();
15
  $tokens = array();
16

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

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

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

    
79
  // Add tokens for the flag count available at the entity level.
80
  // These require token module because we need its helper data and functions
81
  // to deal with token types that are not the same as the entity types they are
82
  // for (in particular, terms and vocabularies).
83
  if (module_exists('token')) {
84
    $entity_info = entity_get_info();
85
    foreach (flag_get_types() as $flag_type) {
86
      // If the flag type is not an entity type then skip.
87
      if (!isset($entity_info[$flag_type])) {
88
        continue;
89
      }
90

    
91
      // The flag type is the entity type, but this is not necessarily the same
92
      // as the entity's token type.
93
      $token_type = $entity_info[$flag_type]['token type'];
94
      $flags = flag_get_flags($flag_type);
95
      foreach ($flags as $flag) {
96
        $tokens[$token_type]['flag-' . str_replace('_', '-', $flag->name) . '-count'] = array(
97
          'name' => t('@flag flag count', array('@flag' => $flag->get_title())),
98
          'description' => t('Total flag count for flag @flag', array('@flag' => $flag->get_title())),
99
          'flag-type' => $flag_type,
100
        );
101
        $tokens[$token_type]['flag-' . str_replace('_', '-', $flag->name) . '-link'] = array(
102
          'name' => t('@flag flag link', array('@flag' => $flag->get_title())),
103
          'description' => t('Flag/unflag link for @flag', array('@flag' => $flag->get_title())),
104
          'flag-type' => $flag_type,
105
        );
106
      }
107
    }
108
  }
109

    
110
  return array(
111
    'types' => $types,
112
    'tokens' => $tokens,
113
  );
114
}
115

    
116
/**
117
 * Implements hook_tokens().
118
 */
119
function flag_tokens($type, $tokens, array $data = array(), array $options = array()) {
120
  $replacements = array();
121
  $sanitize = !empty($options['sanitize']);
122
  $langcode = isset($options['language']) ? $options['language']->language : NULL;
123

    
124
  if ($type == 'flag' && !empty($data['flag'])) {
125
    $flag = $data['flag'];
126
    foreach ($tokens as $name => $original) {
127
      switch ($name) {
128
        case 'name':
129
          $replacements[$original] = $sanitize ? check_plain($flag->name) : $flag->name;
130
          break;
131

    
132
        case 'title':
133
          $replacements[$original] = $sanitize ? check_plain($flag->get_title()) : $flag->get_title();
134
          break;
135
      }
136
    }
137
  }
138
  elseif ($type == 'flagging' && !empty($data['flagging'])) {
139
    $flagging = $data['flagging'];
140
    foreach ($tokens as $name => $original) {
141
      switch ($name) {
142
        case 'date':
143
          $replacements[$original] = format_date($flagging->timestamp, 'medium', '', NULL, $langcode);
144
          break;
145
      }
146
    }
147
    if ($date_tokens = token_find_with_prefix($tokens, 'date')) {
148
      $replacements += token_generate('date', $date_tokens, array('date' => $flagging->timestamp), $options);
149
    }
150
  }
151
  elseif ($type == 'flag-action' && !empty($data['flag-action'])) {
152
    $action = $data['flag-action'];
153
    foreach ($tokens as $name => $original) {
154
      switch ($name) {
155
        case 'action':
156
          $replacements[$original] = $action->action;
157
          break;
158

    
159
        case 'entity-url':
160
          $replacements[$original] = $sanitize ? check_url($action->entity_url) : $action->entity_url;
161
          break;
162

    
163
        case 'entity-title':
164
          $replacements[$original] = $sanitize ? check_plain($action->entity_title) : $action->entity_title;
165
          break;
166

    
167
        case 'entity-type':
168
          $replacements[$original] = $action->entity_type;
169
          break;
170

    
171
        case 'entity-id':
172
          $replacements[$original] = $action->entity_id;
173
          break;
174

    
175
        case 'count':
176
          $replacements[$original] = $action->count;
177
          break;
178
      }
179
    }
180
  }
181

    
182
  // We only provide tokens on entity types if we have token module's helper
183
  // methods available.
184
  if (isset($data[$type]) && module_exists('token')) {
185
    $entity_type = token_get_entity_mapping('token', $type);
186
    if ($entity_type && in_array($entity_type, flag_get_types())) {
187
      $flags = flag_get_flags($entity_type);
188
      $object = $data[$type];
189
      foreach ($flags as $flag) {
190
        foreach ($tokens as $name => $original) {
191
          $flag_count_token = 'flag-' . str_replace('_', '-', $flag->name) . '-count';
192
          $flag_link_token = 'flag-' . str_replace('_', '-', $flag->name) . '-link';
193
          if ($name == $flag_count_token) {
194
            $replacements[$original] = $flag->get_count($flag->get_entity_id($object));
195
          }
196
          elseif ($name == $flag_link_token) {
197
            $replacements[$original] = flag_create_link($flag->name, $flag->get_entity_id($object));
198
          }
199
        }
200
      }
201
    }
202
  }
203
  return $replacements;
204
}
205

    
206
/**
207
 * Returns HTML for a tokens browser.
208
 *
209
 * @param array $variables
210
 *   An associative array containing:
211
 *   - types: An array naming the types of tokens to show.
212
 *   - global_types: Whether to show global tokens.
213
 */
214
function theme_flag_tokens_browser($variables) {
215
  $types = $variables['types'];
216
  $global_types = $variables['global_types'];
217

    
218
  if (module_exists('token')) {
219
    return theme('token_tree', array('token_types' => $types, 'global_types' => $global_types));
220
  }
221
  else {
222
    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>';
223
  }
224
}