Override the display of attachment /Add icons

Overriding the display of attachments -> Adding icons
Tested on Drupal 4.7

This override will add a column of icons corresponding to the the file attached. Hence, the file types (i.e. image, pdf) are more visible to the viewer.

First:

  • Create an 'icons' directory in default 'files' directory.
  • All icons should be of the same type, i.e. (png, gif)
  • Name the icons according to MIME types & sub-types as follows.
    • For image as a type, name the icon -> 'image'.[png|gif]
    • For text as a type, name the icon-> text.[png|gif]
    • For application as a type, name the icon -> sub-type.[png|gif]
    • Example of icons directory listing
      default.gif
      image.gif
      msword.gif
      pdf.gif
      rtf.gif
      text.gif
      vnd.ms-excel.gif
      vnd.ms-powerpoint.gif
      zip.gif

Note mime types look like this [image/jpeg] -> [type/sub-type]

Secondly:

Create template.php in desired theme directory with following code or if one exist append the following code

<?php
/**
* Overide the default theming for attachments
*/
function phptemplate_upload_attachments($files) {
 
$header = array('',t('Attachment'), t('Size'));
 
$rows = array();
  foreach (
$files as $file) {
    if (
$file->list) {
     
$href = check_url(($file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()))));
     
$text = check_plain($file->description ? $file->description : $file->filename);
     
$rows[] = array(theme('file_icon', $file) ,l($text, $href), format_size($file->filesize));
    }
  }
  if (
count($rows)) {
    return
theme('table', $header, $rows, array('id' => 'attachments'));
  }
}


/**
* Theme file icon according to mime type
* an 'icons' directory should be placed inside your default files direcotry
* containing the file icons. Icon names should follow their Mime sub-type.
* Apart from the two generic types 'image' & 'text'
*
* @param file drupal file object
* @return HTML themed icon
*/
function phptemplate_file_icon($file) {
 
//Check if directory icons in current theme exists otherwise, no point, right
 
$icon_ext = '.gif'; //Change your supported format png for example
 
$path     = file_directory_path().'/icons';

  if( !
is_dir($path) ) {
    return;
  }
 
//Get filemime type act accordingly
 
$mime       = explode('/', $file->filemime);
 
$type       = $mime[0];
 
$sub_type   = $mime[1];
 
$output     = '';
 
$defult_icn = $path.'/default'.$icon_ext;
  switch(
$type) {
    case
'image':
     
$img_path = $path.'/image'.$icon_ext;
      if(
is_file($img_path) ){
       
$output .= theme_image($img_path, $file->filename, $file->description);
      }
      else if(
is_file($defult_icn) ) {
       
$output .= theme_image($defult_icn, $file->filename, $file->description);
      }
      break;
    case
'text':
     
$txt_path = $path.'/text'.$icon_ext;
      if(
is_file($txt_path) ){
       
$output .= theme_image($txt_path, $file->filename, $file->description);
      }
      else if(
is_file($defult_icn) ) {
       
$output .= theme_image($defult_icn, $file->filename, $file->description);
      }
      break;
    case
'application':
     
$app_path = $path.'/'.$sub_type.$icon_ext;
      if(
is_file($app_path) ){
       
$output .= theme_image($app_path, $file->filename, $file->description);
      }
      else if(
is_file($defult_icn) ) {
       
$output .= theme_image($defult_icn, $file->filename, $file->description);
      }
      break;
    default:
      if(
is_file($defult_icn) ) {
       
$output .= theme_image($defult_icn, $file->filename, $file->description);
      }
      break;
  }
  return
$output;
}
?>

Change the $icon_ext variable to desired extension

Finally:

Feedback welcome, its my first and hopefully not last humble contribution to the very giving community of drupal.

Thanks seems to work pretty

goose2000 - May 7, 2008 - 16:34

Seems to work pretty good. Note the comments in the code for more info on setup. One thing though, It does not seem to work in IE 6 or 7. No icons show up, hmmm.

Thanks!

 
 

Drupal is a registered trademark of Dries Buytaert.