Issue #1090216: Add MIME type icon compatibility for kml/kmz files.
[project/filefield.git] / filefield.token.inc
CommitLineData
ae22fefe
DP
1<?php
2
3/**
87d3e491
NH
4 * @file
5 * Token hook implementations. Included if token.module is installed.
6 */
7
8/**
dc273ff9
NH
9 * Implementation of hook_token_list().
10 *
11 * Provide a user readable list of FileField tokens.
ae22fefe
DP
12 */
13function filefield_token_list($type = 'all') {
14 if ($type == 'field' || $type == 'all') {
15 $tokens = array();
ee665c76
NH
16 $tokens['file']['filefield-fid'] = t('File ID');
17 $tokens['file']['filefield-description'] = t('File description');
18 $tokens['file']['filefield-filename'] = t('File name');
19 $tokens['file']['filefield-filepath'] = t('File path');
de192ea1 20 $tokens['file']['filefield-fileurl'] = t('File URL');
ee665c76
NH
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');
ae22fefe
DP
27 return $tokens;
28 }
29}
30
31/**
dc273ff9
NH
32 * Implementation of hook_token_values().
33 *
34 * Provide the token values for a given file item.
ae22fefe
DP
35 */
36function filefield_token_values($type, $object = NULL) {
7673e58c 37 $tokens = array();
38 if ($type == 'field' && isset($object[0]['fid'])) {
ae22fefe 39 $item = $object[0];
94bdd499 40 $tokens['filefield-fid'] = $item['fid'];
ac4e6d49
NH
41 $tokens['filefield-description'] = isset($item['data']['description']) ? check_plain($item['data']['description']) : '';
42 $tokens['filefield-filename'] = check_plain($item['filename']);
43 $tokens['filefield-filepath'] = check_plain($item['filepath']);
de192ea1 44 $tokens['filefield-fileurl'] = file_create_url($item['filepath']);
94bdd499
DP
45 $tokens['filefield-filemime'] = $item['filemime'];
46 $tokens['filefield-filesize'] = $item['filesize'];
47 $tokens['filefield-filesize_formatted'] = format_size($item['filesize']);
48 $tokens['filefield-view'] = $item['view'];
45cabd3e
DP
49 $double_extensions = array('tar.gz', 'tar.bz2');
50 foreach ($double_extensions as $ext) {
51 $length = strlen($ext) + 1;
52 $filename = drupal_strtolower($item['filename']);
53 $pos = strrpos($filename, '.'. $ext);
54 if ($pos !== FALSE && strlen($filename) - $pos == $length) {
55 $tokens['filefield-onlyname'] = substr($item['filename'], 0, $pos);
56 $tokens['filefield-extension'] = $ext;
57 break;
58 }
59 }
60 if (!isset($tokens['filefield-extension'])) {
61 $info = pathinfo($item['filename']);
fa7b2393 62 $tokens['filefield-onlyname'] = basename($info['basename'], '.'. $info['extension']);
45cabd3e
DP
63 $tokens['filefield-extension'] = $info['extension'];
64 }
ae22fefe 65 }
7673e58c 66 return $tokens;
ae22fefe 67}