5 * Theme functions used for normal file output.
7 * Uses content.module to store the fid and field specific metadata,
8 * and Drupal's {files} table to store the actual file data.
12 * Return an image with an appropriate icon for the given file.
15 * A file object for which to make an icon.
17 function theme_filefield_icon($file) {
18 if (is_object($file)) {
19 $file = (array) $file;
21 $mime = check_plain($file['filemime']);
23 $dashed_mime = strtr($mime, array('/' => '-', '+' => '-'));
25 if ($icon_url = filefield_icon_url($file)) {
26 return '<img class="filefield-icon field-icon-'.
$dashed_mime .
'" alt="'.
t('@mime icon', array('@mime' => $mime)) .
'" src="'.
$icon_url .
'" />';
31 * Given a file object, create a URL to a matching icon.
36 * Optional. The theme to be used for the icon. Defaults to the value of
37 * the "filefield_icon_theme" variable.
39 * A URL string to the icon, or FALSE if an appropriate icon could not be
42 function _filefield_icon_url($file, $theme = NULL
) {
45 if ($icon_path = _filefield_icon_path($file, $theme)) {
46 return $base_url .
'/'.
$icon_path;
52 * Given a file object, create a URL to a matching icon.
57 * Optional. The theme to be used for the icon. Defaults to the value of
58 * the "filefield_icon_theme" variable.
60 * A string to the icon as a local path, or FALSE if an appropriate icon could
63 function _filefield_icon_path($file, $theme = NULL
) {
65 $theme = variable_get('filefield_icon_theme', 'default');
68 // If there's an icon matching the exact mimetype, go for it.
69 $dashed_mime = strtr($file['filemime'], array('/' => '-'));
70 if ($icon_path = _filefield_create_icon_path($dashed_mime, $theme)) {
73 // For a couple of mimetypes, we can "manually" tell a generic icon.
74 if ($generic_name = _filefield_generic_icon_map($file)) {
75 if ($icon_path = _filefield_create_icon_path($generic_name, $theme)) {
79 // Use generic icons for each category that provides such icons.
80 foreach (array('audio', 'image', 'text', 'video') as
$category) {
81 if (strpos($file['filemime'], $category .
'/') === 0) {
82 if ($icon_path = _filefield_create_icon_path($category .
'-x-generic', $theme)) {
87 // Try application-octet-stream as last fallback.
88 if ($icon_path = _filefield_create_icon_path('application-octet-stream', $theme)) {
91 // Sorry, no icon can be found...
96 * Internal function to convert a file icon theme name to a directory.
98 function _filefield_icon_directory($theme = NULL
) {
102 $sets = module_invoke_all('filefield_icon_sets');
103 drupal_alter('filefield_icon_sets', $sets);
106 if (!isset($theme) || !isset($sets[$theme])) {
110 return $sets[$theme];
113 function _filefield_create_icon_path($icon_name, $theme = NULL
) {
114 $icons_directory = _filefield_icon_directory($theme);
115 $icon_path = $icons_directory .
'/'.
$icon_name .
'.png';
117 if (file_exists($icon_path)) {
123 function _filefield_generic_icon_map($file) {
124 switch ($file['filemime']) {
125 // Word document types.
126 case
'application/msword':
127 case
'application/vnd.ms-word.document.macroEnabled.12':
128 case
'application/vnd.oasis.opendocument.text':
129 case
'application/vnd.oasis.opendocument.text-template':
130 case
'application/vnd.oasis.opendocument.text-master':
131 case
'application/vnd.oasis.opendocument.text-web':
132 case
'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
133 case
'application/vnd.stardivision.writer':
134 case
'application/vnd.sun.xml.writer':
135 case
'application/vnd.sun.xml.writer.template':
136 case
'application/vnd.sun.xml.writer.global':
137 case
'application/vnd.wordperfect':
138 case
'application/x-abiword':
139 case
'application/x-applix-word':
140 case
'application/x-kword':
141 case
'application/x-kword-crypt':
142 return 'x-office-document';
144 // Spreadsheet document types.
145 case
'application/vnd.ms-excel':
146 case
'application/vnd.ms-excel.sheet.macroEnabled.12':
147 case
'application/vnd.oasis.opendocument.spreadsheet':
148 case
'application/vnd.oasis.opendocument.spreadsheet-template':
149 case
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
150 case
'application/vnd.stardivision.calc':
151 case
'application/vnd.sun.xml.calc':
152 case
'application/vnd.sun.xml.calc.template':
153 case
'application/vnd.lotus-1-2-3':
154 case
'application/x-applix-spreadsheet':
155 case
'application/x-gnumeric':
156 case
'application/x-kspread':
157 case
'application/x-kspread-crypt':
158 return 'x-office-spreadsheet';
160 // Presentation document types.
161 case
'application/vnd.ms-powerpoint':
162 case
'application/vnd.ms-powerpoint.presentation.macroEnabled.12':
163 case
'application/vnd.oasis.opendocument.presentation':
164 case
'application/vnd.oasis.opendocument.presentation-template':
165 case
'application/vnd.openxmlformats-officedocument.presentationml.presentation':
166 case
'application/vnd.openxmlformats-officedocument.presentationml.slideshow':
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/rtf':
235 case
'application/x-macbinary':
236 case
'application/x-ms-dos-executable':
237 case
'application/x-pef-executable':
238 return 'application-x-executable';