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