Project

General

Profile

Paste
Download (10.9 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / file_entity / file_entity.tokens.inc @ 082b75eb

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
          $info['tokens'][$entity_type][$field_name] = array(
114
            'name' => $field_info['label'],
115
            'description' => $field_info['description'],
116
            'type' => 'file_field',
117
            'module' => 'file_entity',
118
          );
119
        }
120
      }
121
    }
122
  }
123

    
124
  return $info;
125
}
126

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

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

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

    
144
    foreach ($tokens as $name => $original) {
145

    
146
      // Split out the token into its parts
147
      $parts = explode(':', $name, 3);
148

    
149
      $field_name    = $parts[0];
150
      $property      = (isset($parts[1])) ? $parts[1] : '';
151
      $array_handler = (isset($parts[2])) ? $parts[2] : '';
152

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

    
156
        // Get basic information
157
        $entity_type = $data['entity_type'];
158
        $langcode = isset($options['language']) ? $options['language']->language : NULL;
159
        $entity = clone $data['entity'];
160

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

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

    
186
        // Unset clone of entity
187
        unset($entity);
188
      }
189
    }
190
  }
191

    
192
  $url_options = array('absolute' => TRUE);
193
  if (isset($options['language'])) {
194
    $url_options['language'] = $options['language'];
195
    $language_code = $options['language']->language;
196
  }
197
  else {
198
    $language_code = NULL;
199
  }
200

    
201
  $sanitize = !empty($options['sanitize']);
202

    
203
  // File tokens.
204
  if ($type == 'file' && !empty($data['file'])) {
205
    $file = $data['file'];
206

    
207
    foreach ($tokens as $name => $original) {
208
      switch ($name) {
209
        case 'type':
210
          if ($file_type = file_type_load($file->type)) {
211
            $replacements[$original] = $sanitize ? check_plain($file_type->label) : $file_type->label;
212
          }
213
          break;
214

    
215
        case 'download-url':
216
          $uri = file_entity_download_uri($file);
217
          $replacements[$original] = url($uri['path'], $uri['options'] + $url_options);
218
          break;
219
      }
220
    }
221

    
222
    // Chained token relationships.
223
    if (($file_type_tokens = token_find_with_prefix($tokens, 'type')) && $file_type = file_type_load($file->type)) {
224
      $replacements += token_generate('file-type', $file_type_tokens, array('file_type' => $file_type), $options);
225
    }
226
    if ($download_url_tokens = token_find_with_prefix($tokens, 'download-url')) {
227
      $replacements += token_generate('url', $download_url_tokens, file_entity_download_uri($file), $options);
228
    }
229
  }
230

    
231
  // File type tokens.
232
  if ($type == 'file-type' && !empty($data['file_type'])) {
233
    $file_type = $data['file_type'];
234

    
235
    foreach ($tokens as $name => $original) {
236
      switch ($name) {
237
        case 'name':
238
          $replacements[$original] = $sanitize ? check_plain($file_type->label) : $file_type->label;
239
          break;
240

    
241
        case 'machine-name':
242
          // This is a machine name so does not ever need to be sanitized.
243
          $replacements[$original] = $file_type->type;
244
          break;
245

    
246
        case 'count':
247
          $query = db_select('file_managed');
248
          $query->condition('type', $file_type->type);
249
          $query->addTag('file_type_file_count');
250
          $count = $query->countQuery()->execute()->fetchField();
251
          $replacements[$original] = (int) $count;
252
          break;
253

    
254
        case 'edit-url':
255
          $replacements[$original] = url('admin/structure/file-types/manage/' . $file_type->type . '/fields', $url_options);
256
          break;
257
      }
258
    }
259
  }
260

    
261
  return $replacements;
262
}
263

    
264
/**
265
 * This helper function gets file properties for token replacement.
266
 *
267
 * @param array $files
268
 * An array of files that are values for the field.
269
 *
270
 * @param string $property
271
 * The property to retrieve from the file info. See file_entity_token_info() for
272
 * a list of properties.
273
 *
274
 * @param string $array_handler
275
 * The optional array modifier, e.g. "count" or "join:,".
276
 *
277
 * @return mixed
278
 * Either a single value, the first of the array, or an array of values.
279
 */
280
function _file_entity_tokens_get_property($files, $property, $array_handler = 'first') {
281

    
282
  // If we only need the first variable
283
  $return_first = ($array_handler == 'first' || empty($array_handler) || $array_handler == 'value:0');
284

    
285
  // This static variable stores image info
286
  $info = &drupal_static(__FUNCTION__);
287

    
288
  foreach ($files as $file) {
289
    $file['url'] = file_create_url($file['uri']);
290
    $file['https-url'] = str_replace('http://', 'https://', $file['url']);
291

    
292
    // If values are: filename, filemime, type, url, https-url
293
    if (isset($file[$property])) {
294
      $value = $file[$property];
295
    }
296

    
297
    // If values are: image, height, width, https-image
298
    elseif (!empty($info[$file['fid']])) {
299
      $value = $info[$file['fid']][$property] ?: NULL;
300
    }
301
    // If values are files types
302
    else {
303

    
304
      // If file type is image
305
      if ($file['type'] == 'image') {
306
        $imageuri = $file['uri'];
307
      }
308

    
309
      // If file type is video
310
      elseif ($file['type'] == 'video') {
311
        list($provider, $filename) = explode('://v/', $file['uri']);
312
        $imageuri = "public://file_entity-$provider/$filename.jpg";
313
      }
314

    
315
      // Do nothing for other file types
316
      // @todo: Other file types may need handling
317
      else {
318
        $imageuri = FALSE;
319
      }
320

    
321
      if ($info[$file['fid']] = image_get_info($imageuri)) {
322
        $info[$file['fid']]['image'] = file_create_url($imageuri);
323
        $info[$file['fid']]['https-image'] = str_replace('http://', 'https://', $info[$file['fid']]['image']);
324
      }
325

    
326
      $value = $info[$file['fid']][$property] ?: NULL;
327
    }
328

    
329
    if ($return_first) {
330
      return $value;
331
    }
332
    $values[] = $value;
333
  }
334

    
335
  return $values;
336
}