Moving FileField Token to an .inc file instead of a separate module.
[project/filefield.git] / filefield.token.inc
1 <?php
2
3 /**
4 * Implementation of hook_token_list():
5 * Provide a user readable list of filefield tokens.
6 */
7 function filefield_token_list($type = 'all') {
8 if ($type == 'field' || $type == 'all') {
9 $tokens = array();
10 $tokens['file']['filefield-fid'] = t("File ID");
11 $tokens['file']['filefield-description'] = t("File description");
12 $tokens['file']['filefield-filename'] = t("File name");
13 $tokens['file']['filefield-filepath'] = t("File path");
14 $tokens['file']['filefield-filemime'] = t("File MIME type");
15 $tokens['file']['filefield-filesize'] = t("File size (in bytes)");
16 $tokens['file']['filefield-filesize_formatted'] = t("File size (pretty printed)");
17 $tokens['file']['filefield-view'] = t("Fully formatted HTML file tag");
18 $tokens['file']['filefield-onlyname'] = t("File name without extension.");
19 $tokens['file']['filefield-extension'] = t("File extension");
20 return $tokens;
21 }
22 }
23
24 /**
25 * Implementation of hook_token_values():
26 * Provide the concrete token values for a given file item.
27 */
28 function filefield_token_values($type, $object = NULL) {
29 if ($type == 'field') {
30 $item = $object[0];
31 $tokens['filefield-fid'] = $item['fid'];
32 $tokens['filefield-description'] = isset($item['data']['description']) ? $item['data']['description'] : '';
33 $tokens['filefield-filename'] = $item['filename'];
34 $tokens['filefield-filepath'] = $item['filepath'];
35 $tokens['filefield-filemime'] = $item['filemime'];
36 $tokens['filefield-filesize'] = $item['filesize'];
37 $tokens['filefield-filesize_formatted'] = format_size($item['filesize']);
38 $tokens['filefield-view'] = $item['view'];
39 $double_extensions = array('tar.gz', 'tar.bz2');
40 foreach ($double_extensions as $ext) {
41 $length = strlen($ext) + 1;
42 $filename = drupal_strtolower($item['filename']);
43 $pos = strrpos($filename, '.'. $ext);
44 if ($pos !== FALSE && strlen($filename) - $pos == $length) {
45 $tokens['filefield-onlyname'] = substr($item['filename'], 0, $pos);
46 $tokens['filefield-extension'] = $ext;
47 break;
48 }
49 }
50 if (!isset($tokens['filefield-extension'])) {
51 $info = pathinfo($item['filename']);
52 $tokens['filefield-onlyname'] = $info['filename'];
53 $tokens['filefield-extension'] = $info['extension'];
54 }
55 return $tokens;
56 }
57 }