Projet

Général

Profil

Paste
Télécharger (11,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / file_entity / file_entity.tokens.inc @ 66c11afc

1
<?php
2

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

    
8
/**
9
 * Implements hook_token_info().
10
 */
11
function file_entity_token_info() {
12
  // File type tokens.
13
  $info['types']['file-type'] = array(
14
    'name' => t('File type'),
15
    'description' => t('Tokens associated with file types.'),
16
    'needs-data' => 'file_type',
17
  );
18
  $info['tokens']['file-type']['name'] = array(
19
    'name' => t('Name'),
20
    'description' => t('The name of the file type.'),
21
  );
22
  $info['tokens']['file-type']['machine-name'] = array(
23
    'name' => t('Machine-readable name'),
24
    'description' => t('The unique machine-readable name of the file type.'),
25
  );
26
  $info['tokens']['file-type']['count'] = array(
27
    'name' => t('File count'),
28
    'description' => t('The number of files belonging to the file type.'),
29
  );
30
  $info['tokens']['file-type']['edit-url'] = array(
31
    'name' => t('Edit URL'),
32
    'description' => t("The URL of the file type's edit page."),
33
  );
34

    
35
  // File tokens.
36
  $info['tokens']['file']['type'] = array(
37
    'name' => t('File type'),
38
    'description' => t('The file type of the file.'),
39
    'type' => 'file-type',
40
  );
41
  $info['tokens']['file']['download-url'] = array(
42
    'name' => t('Download URL'),
43
    'description' => t('The URL to download the file directly.'),
44
    'type' => 'url',
45
  );
46

    
47
  if (module_exists('token')) {
48
    $info['types']['file_field'] = array(
49
      'name' => t('Media'),
50
      'description' => t('Tokens related to a file_entity field.'),
51
      'hidden' => TRUE,
52
    );
53

    
54
    $default_text = ' ' . t('Defaults to first value.');
55

    
56
    $info['tokens']['file_field'] = array(
57
      'field' => array(
58
        'name' => t('Field token value'),
59
        'description' => t('Default: The value returned by the token field formatter.') . $default_text,
60
      ),
61
      'url' => array(
62
        'name' => t('URL'),
63
        'description' => t('URL of the file_entity resource.') . $default_text,
64
        'type' => 'array',
65
      ),
66
      'filename' => array(
67
        'name' => t('Filename'),
68
        'description' => t('Filename the file_entity resource.') . $default_text,
69
        'type' => 'array',
70
      ),
71
      'filemime' => array(
72
        'name' => t('MIME type'),
73
        'description' => t('MIME type of the file_entity resource.') . $default_text,
74
        'type' => 'array',
75
      ),
76
      'type' => array(
77
        'name' => t('File type'),
78
        'description' => t('File type of the file_entity resource.') . $default_text,
79
        'type' => 'array',
80
      ),
81
      'image' => array(
82
        'name' => t('Image'),
83
        'description' => t('URL of a representative image for the file_entity resource, e.g. a video thumbnail.') . $default_text,
84
        'type' => 'array',
85
      ),
86
      'height' => array(
87
        'name' => t('Height'),
88
        'description' => t('Height of the file_entity resource, for videos or images.') . $default_text,
89
        'type' => 'array',
90
      ),
91
      'width' => array(
92
        'name' => t('Width'),
93
        'description' => t('Width of the file_entity resource, for videos or images.') . $default_text,
94
        'type' => 'array',
95
      ),
96
      'https-url' => array(
97
        'name' => t('Secure URL'),
98
        'description' => t('URL of the file_entity resource using HTTPS.') . $default_text,
99
        'type' => 'array',
100
      ),
101
      'https-image' => array(
102
        'name' => t('Secure image'),
103
        'description' => t('URL of a representative image for the file_entity resource using HTTPS, usually for videos.') . $default_text,
104
        'type' => 'array',
105
      ),
106
    );
107

    
108
    $all_fields = field_info_field_map();
109
    foreach ($all_fields as $field_name => $field) {
110
      if ($field['type'] == 'file') {
111
        $field_info = _token_field_info($field_name);
112
        foreach (array_keys($field['bundles']) as $entity_type) {
113
          if ($entity_type == 'taxonomy_term') {
114
            $entity_type = 'term';
115
          }
116
          $info['tokens'][$entity_type][$field_name] = array(
117
            'name' => $field_info['label'],
118
            'description' => $field_info['description'],
119
            'type' => 'file_field',
120
            'module' => 'file_entity',
121
          );
122
        }
123
      }
124
    }
125
  }
126

    
127
  return $info;
128
}
129

    
130
/**
131
 * Implements hook_token_info_alter().
132
 */
133
function file_entity_token_info_alter(&$info) {
134
  $info['tokens']['file']['name']['description'] = t('The name of the file.');
135
}
136

    
137
/**
138
 * Provide replacement values for placeholder tokens.
139
 */
140
function file_entity_tokens($type, $tokens, array $data = array(), array $options = array()) {
141
  $replacements = array();
142

    
143
  // Check that this token call contains the data we need
144
  if ($type == 'entity' && !empty($data['entity_type']) && !empty($data['entity']) &&
145
    !empty($data['token_type']) && module_exists('token')) {
146

    
147
    foreach ($tokens as $name => $original) {
148

    
149
      // Split out the token into its parts
150
      $parts = explode(':', $name, 3);
151

    
152
      $field_name    = $parts[0];
153
      $property      = (isset($parts[1])) ? $parts[1] : '';
154
      $array_handler = (isset($parts[2])) ? $parts[2] : '';
155

    
156
      // Check that the field has content and that we should handle it
157
      if (!empty($data['entity']->$field_name) && _token_module($data['token_type'], $field_name) == 'file_entity') {
158

    
159
        // Get basic information
160
        $entity_type = $data['entity_type'];
161
        if ($entity_type == 'taxonomy_term') {
162
          $entity_type = 'term';
163
        }
164
        $langcode = isset($options['language']) ? $options['language']->language : NULL;
165
        $entity = clone $data['entity'];
166

    
167
        // If we are looking for the field output, let field module handle it
168
        if (empty($property) || $property == 'field') {
169
          unset($entity->_field_view_prepared);
170
          $field_output = field_view_field($entity_type, $entity, $field_name, 'token', $langcode);
171
          $field_output['#token_options'] = $options;
172
          $field_output['#prerender'][] = 'token_pre_render_field_token';
173
          $replacements[$original] = drupal_render($field_output);
174
        }
175
        else {
176
          $items = field_get_items($entity_type, $entity, $field_name);
177
          $return = _file_entity_tokens_get_property($items, $property, $array_handler);
178

    
179
          // We may get a single value or an array.
180
          // Handle array with the array function from token module.
181
          if (is_array($return)) {
182
            $search_tokens = token_find_with_prefix($tokens, $field_name);
183
            if ($array_tokens = token_find_with_prefix($search_tokens, $property)) {
184
              $replacements += token_generate('array', $array_tokens, array('array' => $return), $options);
185
            }
186
          }
187
          else {
188
            $replacements[$original] = $return;
189
          }
190
        }
191

    
192
        // Unset clone of entity
193
        unset($entity);
194
      }
195
    }
196
  }
197

    
198
  $url_options = array('absolute' => TRUE);
199
  if (isset($options['language'])) {
200
    $url_options['language'] = $options['language'];
201
    $language_code = $options['language']->language;
202
  }
203
  else {
204
    $language_code = NULL;
205
  }
206

    
207
  $sanitize = !empty($options['sanitize']);
208

    
209
  // File tokens.
210
  if ($type == 'file' && !empty($data['file'])) {
211
    $file = $data['file'];
212

    
213
    foreach ($tokens as $name => $original) {
214
      switch ($name) {
215
        case 'type':
216
          if ($file_type = file_type_load($file->type)) {
217
            $replacements[$original] = $sanitize ? check_plain($file_type->label) : $file_type->label;
218
          }
219
          break;
220

    
221
        case 'download-url':
222
          $uri = file_entity_download_uri($file);
223
          $replacements[$original] = url($uri['path'], $uri['options'] + $url_options);
224
          break;
225
      }
226
    }
227

    
228
    // Chained token relationships.
229
    if (($file_type_tokens = token_find_with_prefix($tokens, 'type')) && $file_type = file_type_load($file->type)) {
230
      $replacements += token_generate('file-type', $file_type_tokens, array('file_type' => $file_type), $options);
231
    }
232
    if ($download_url_tokens = token_find_with_prefix($tokens, 'download-url')) {
233
      $replacements += token_generate('url', $download_url_tokens, file_entity_download_uri($file), $options);
234
    }
235
  }
236

    
237
  // File type tokens.
238
  if ($type == 'file-type' && !empty($data['file_type'])) {
239
    $file_type = $data['file_type'];
240

    
241
    foreach ($tokens as $name => $original) {
242
      switch ($name) {
243
        case 'name':
244
          $replacements[$original] = $sanitize ? check_plain($file_type->label) : $file_type->label;
245
          break;
246

    
247
        case 'machine-name':
248
          // This is a machine name so does not ever need to be sanitized.
249
          $replacements[$original] = $file_type->type;
250
          break;
251

    
252
        case 'count':
253
          $query = db_select('file_managed');
254
          $query->condition('type', $file_type->type);
255
          $query->addTag('file_type_file_count');
256
          $count = $query->countQuery()->execute()->fetchField();
257
          $replacements[$original] = (int) $count;
258
          break;
259

    
260
        case 'edit-url':
261
          $replacements[$original] = url('admin/structure/file-types/manage/' . $file_type->type . '/fields', $url_options);
262
          break;
263
      }
264
    }
265
  }
266

    
267
  return $replacements;
268
}
269

    
270
/**
271
 * This helper function gets file properties for token replacement.
272
 *
273
 * @param array $files
274
 * An array of files that are values for the field.
275
 *
276
 * @param string $property
277
 * The property to retrieve from the file info. See file_entity_token_info() for
278
 * a list of properties.
279
 *
280
 * @param string $array_handler
281
 * The optional array modifier, e.g. "count" or "join:,".
282
 *
283
 * @return mixed
284
 * Either a single value, the first of the array, or an array of values.
285
 */
286
function _file_entity_tokens_get_property($files, $property, $array_handler = 'first') {
287
  $value = NULL;
288
  // If we only need the first variable
289
  $return_first = ($array_handler == 'first' || empty($array_handler) || $array_handler == 'value:0');
290

    
291
  // This static variable stores image info
292
  $info = &drupal_static(__FUNCTION__);
293

    
294
  foreach ($files as $file) {
295
    $file['url'] = file_create_url($file['uri']);
296
    $file['https-url'] = str_replace('http://', 'https://', $file['url']);
297

    
298
    // If values are: filename, filemime, type, url, https-url
299
    if (isset($file[$property])) {
300
      $value = $file[$property];
301
    }
302

    
303
    // If values are: image, height, width, https-image
304
    elseif (!empty($info[$file['fid']])) {
305
      if (isset($info[$file['fid']][$property])) {
306
        $value = $info[$file['fid']][$property];
307
      } else {
308
        $value = NULL;
309
      }
310
    }
311
    // If values are files types
312
    else {
313

    
314
      // If file type is image
315
      if ($file['type'] == 'image') {
316
        $imageuri = $file['uri'];
317
      }
318

    
319
      // If file type is video
320
      elseif ($file['type'] == 'video') {
321
        list($provider, $filename) = explode('://v/', $file['uri']);
322
        $imageuri = "public://file_entity-$provider/$filename.jpg";
323
      }
324

    
325
      // Do nothing for other file types
326
      // @todo: Other file types may need handling
327
      else {
328
        $imageuri = FALSE;
329
      }
330

    
331
      if ($info[$file['fid']] = image_get_info($imageuri)) {
332
        $info[$file['fid']]['image'] = file_create_url($imageuri);
333
        $info[$file['fid']]['https-image'] = str_replace('http://', 'https://', $info[$file['fid']]['image']);
334
      }
335

    
336
      if (isset($info[$file['fid']][$property])) {
337
        $value = $info[$file['fid']][$property];
338
      } else {
339
        $value = NULL;
340
      }
341
    }
342

    
343
    if ($return_first) {
344
      return $value;
345
    }
346
    $values[] = $value;
347
  }
348

    
349
  return $values;
350
}