6 * Theme functions used for normal file output.
8 * Uses content.module to store the fid and field specific metadata,
9 * and Drupal's {files} table to store the actual file data.
13 * Return an image with an appropriate icon for the given file.
16 * A file object for which to make an icon.
18 function theme_filefield_icon($file) {
19 if (is_object($file)) {
20 $file = (array) $file;
22 $mime = check_plain($file['filemime']);
24 $dashed_mime = strtr($mime, array('/' => '-', '+' => '-'));
26 if ($icon_url = filefield_icon_url($file)) {
27 return '<img class="filefield-icon field-icon-'.
$dashed_mime .
'" alt="'.
t('@mime icon', array('@mime' => $mime)) .
'" src="'.
$icon_url .
'" />';
32 * Given a file object, create a URL to a matching icon.
37 * Optional. The theme to be used for the icon. Defaults to the value of
38 * the "filefield_icon_theme" variable.
40 * A URL string to the icon, or FALSE if an appropriate icon could not be
43 function _filefield_icon_url($file, $theme = NULL
) {
46 if ($icon_path = _filefield_icon_path($file, $theme)) {
47 return $base_url .
'/'.
$icon_path;
53 * Given a file object, create a URL to a matching icon.
58 * Optional. The theme to be used for the icon. Defaults to the value of
59 * the "filefield_icon_theme" variable.
61 * A string to the icon as a local path, or FALSE if an appropriate icon could
64 function _filefield_icon_path($file, $theme = NULL
) {
66 $theme = variable_get('filefield_icon_theme', 'default');
69 // If there's an icon matching the exact mimetype, go for it.
70 $dashed_mime = strtr($file['filemime'], array('/' => '-'));
71 if ($icon_path = _filefield_create_icon_path($dashed_mime, $theme)) {
74 // For a couple of mimetypes, we can "manually" tell a generic icon.
75 if ($generic_name = _filefield_generic_icon_map($file)) {
76 if ($icon_path = _filefield_create_icon_path($generic_name, $theme)) {
80 // Use generic icons for each category that provides such icons.
81 foreach (array('audio', 'image', 'text', 'video') as
$category) {
82 if (strpos($file['filemime'], $category .
'/') === 0) {
83 if ($icon_path = _filefield_create_icon_path($category .
'-x-generic', $theme)) {
88 // Try application-octet-stream as last fallback.
89 if ($icon_path = _filefield_create_icon_path('application-octet-stream', $theme)) {
92 // Sorry, no icon can be found...
97 * Internal function to convert a file icon theme name to a directory.
99 function _filefield_icon_directory($theme = NULL
) {
103 $sets = module_invoke_all('filefield_icon_sets');
104 drupal_alter('filefield_icon_sets', $sets);
107 if (!isset($theme) || !isset($sets[$theme])) {
111 return $sets[$theme];
114 function _filefield_create_icon_path($icon_name, $theme = NULL
) {
115 $icons_directory = _filefield_icon_directory($theme);
116 $icon_path = $icons_directory .
'/'.
$icon_name .
'.png';
118 if (file_exists($icon_path)) {
124 function _filefield_generic_icon_map($file) {
125 switch ($file['filemime']) {
126 // Word document types.
127 case
'application/msword':
128 case
'application/vnd.ms-word.document.macroEnabled.12':
129 case
'application/vnd.oasis.opendocument.text':
130 case
'application/vnd.oasis.opendocument.text-template':
131 case
'application/vnd.oasis.opendocument.text-master':
132 case
'application/vnd.oasis.opendocument.text-web':
133 case
'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
134 case
'application/vnd.stardivision.writer':
135 case
'application/vnd.sun.xml.writer':
136 case
'application/vnd.sun.xml.writer.template':
137 case
'application/vnd.sun.xml.writer.global':
138 case
'application/vnd.wordperfect':
139 case
'application/x-abiword':
140 case
'application/x-applix-word':
141 case
'application/x-kword':
142 case
'application/x-kword-crypt':
143 return 'x-office-document';
145 // Spreadsheet document types.
146 case
'application/vnd.ms-excel':
147 case
'application/vnd.ms-excel.sheet.macroEnabled.12':
148 case
'application/vnd.oasis.opendocument.spreadsheet':
149 case
'application/vnd.oasis.opendocument.spreadsheet-template':
150 case
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
151 case
'application/vnd.stardivision.calc':
152 case
'application/vnd.sun.xml.calc':
153 case
'application/vnd.sun.xml.calc.template':
154 case
'application/vnd.lotus-1-2-3':
155 case
'application/x-applix-spreadsheet':
156 case
'application/x-gnumeric':
157 case
'application/x-kspread':
158 case
'application/x-kspread-crypt':
159 return 'x-office-spreadsheet';
161 // Presentation document types.
162 case
'application/vnd.ms-powerpoint':
163 case
'application/vnd.ms-powerpoint.presentation.macroEnabled.12':
164 case
'application/vnd.oasis.opendocument.presentation':
165 case
'application/vnd.oasis.opendocument.presentation-template':
166 case
'application/vnd.openxmlformats-officedocument.presentationml.presentation':
167 case
'application/vnd.stardivision.impress':
168 case
'application/vnd.sun.xml.impress':
169 case
'application/vnd.sun.xml.impress.template':
170 case
'application/x-kpresenter':
171 return 'x-office-presentation';
173 // Compressed archive types.
174 case
'application/zip':
175 case
'application/x-zip':
176 case
'application/stuffit':
177 case
'application/x-stuffit':
178 case
'application/x-7z-compressed':
179 case
'application/x-ace':
180 case
'application/x-arj':
181 case
'application/x-bzip':
182 case
'application/x-bzip-compressed-tar':
183 case
'application/x-compress':
184 case
'application/x-compressed-tar':
185 case
'application/x-cpio-compressed':
186 case
'application/x-deb':
187 case
'application/x-gzip':
188 case
'application/x-java-archive':
189 case
'application/x-lha':
190 case
'application/x-lhz':
191 case
'application/x-lzop':
192 case
'application/x-rar':
193 case
'application/x-rpm':
194 case
'application/x-tzo':
195 case
'application/x-tar':
196 case
'application/x-tarz':
197 case
'application/x-tgz':
198 return 'package-x-generic';
200 // Script file types.
201 case
'application/ecmascript':
202 case
'application/javascript':
203 case
'application/mathematica':
204 case
'application/vnd.mozilla.xul+xml':
205 case
'application/x-asp':
206 case
'application/x-awk':
207 case
'application/x-cgi':
208 case
'application/x-csh':
209 case
'application/x-m4':
210 case
'application/x-perl':
211 case
'application/x-php':
212 case
'application/x-ruby':
213 case
'application/x-shellscript':
214 case
'text/vnd.wap.wmlscript':
215 case
'text/x-emacs-lisp':
216 case
'text/x-haskell':
217 case
'text/x-literate-haskell':
219 case
'text/x-makefile':
220 case
'text/x-matlab':
221 case
'text/x-python':
224 return 'text-x-script';
227 case
'application/xhtml+xml':
231 case
'application/x-macbinary':
232 case
'application/x-ms-dos-executable':
233 case
'application/x-pef-executable':
234 return 'application-x-executable';