21 August 2013

Change or add file icons to Drupal 7. First you have to define your mime type of a file, if it isn't done already. Then add icon URL to that mime type (16x16).

Define mime type

Define your mime type if it haven't been defined already.
Source code viewer
  1. function HOOK_file_mimetype_mapping_alter(&$mapping) {
  2. $new_mappings['epub'] = 'application/epub+zip';
  3. foreach ($new_mappings as $extension => $mime_type) {
  4. if (!in_array($mime_type, $mapping['mimetypes'])) {
  5. // If the mime type does not already exist, add it.
  6. $mapping['mimetypes'][] = $mime_type;
  7. }
  8.  
  9. $index = array_search($mime_type, $mapping['mimetypes']);
  10. $mapping['extensions'][$extension] = $index;
  11. }
  12. }
Programming Language: PHP

Attach image url to mime type

Source code viewer
  1. function THEME_file_icon($variables) {
  2. global $base_url;
  3.  
  4. $file = $variables['file'];
  5. $icon_directory = $variables['icon_directory'];
  6.  
  7. $mime = check_plain($file->filemime);
  8. if ($mime == 'application/epub+zip') {
  9. $icon_url = $base_url . base_path() . drupal_get_path('theme', 'ep') . '/img/ico-epub.png';
  10. }
  11. else {
  12. $icon_url = file_icon_url($file, $icon_directory);
  13. }
  14.  
  15. return '<img class="file-icon" alt="" title="' . $mime . '" src="' . $icon_url . '" />';
  16. }
Programming Language: PHP