Agaric Design Collective

Theming the File Attachments in Drupal 5.x

Average: 4.5 (4 votes)
By Dan Hakimzadeh
on 27 Mar
10 comments

Key words and phrases

theming drupal attachment file mimetype icons

Tags

Description

Alrighty then,

If you're reading this then you've probably been searching for a way to theme the default file attachment table in drupal and add cool icons according to the file's mime type, (ie. if it is an mp3 then a music icon will show, if a pdf then a pdf icon, etc)

This is a quick and easy method to help you get this done on your sites and make people think you are cool...

so first you gonna want some icons... I have attached some basic ones below, but get creative, use whatever you like... (if you are really proud of them I'd like to check them out, maybe I'll even add them here and credit you!)

btw, this is a modded version of stuff that can be found here -> http://drupal.org/node/86413
but there are some improvements, well, at least in my opinion...

[This technique is an override of the themeable function theme_upload_attachments.]

anyway, onto the fun stuff,

Resolution

first off, grab the icon files you want to use and put them in a folder called 'icons' in your theme directory.
here is a breakdown how the files should be named, so that they display with the right filetype:

default.gif
image.gif
msword.gif
pdf.gif
rtf.gif
text.gif
vnd.ms-excel.gif
vnd.ms-powerpoint.gif
zip.gif

ok, that that you've got that, all you have to do is open up your theme's template.php file and paste in the code below:

<?php
function phptemplate_upload_attachments($files) {
 
$header = array(t('Attachment'), t('(click to download)'));
 
$rows = array();
  foreach (
$files as $file) {
   
$file = (object)$file;
    if (
$file->list && !$file->remove) {
     
// Generate valid URL for both existing attachments and preview of new attachments (these have 'upload' in fid)
     
$href = file_create_url((strpos($file->fid, 'upload') === FALSE ? $file->filepath : file_create_filename($file->filename, file_create_path())));
     
$text = $file->description ? $file->description : $file->filename;
     
$rows[] = array(theme('file_icon', $file) ,l($text, $href));
    }
  }
  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 = '.png'; //Change your supported format png for example
 
$path     = path_to_theme().'/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;
}
?>

that's all folks...

 

+1

very well written. :-)

loved the comment in the code

otherwise, no point, right

Posted by o0mav0o (not verified) on Tue, 2008-04-08 21:30
File Sizes

So it has come to my attention that some people may want the filesize to show for each attachment as well. This was originally functionality that I took out because it didn't fit in with the project I was working on.. blah blah blah... the point is, I'll revise this post soon to give the option of adding it back.

Posted by Dan Hakimzadeh on Mon, 2008-04-14 15:39
Where are the attached files?

The files attached above are no longer found. Will you move them to the right place?

Thanks

Amnon

Posted by Amnon Levav (not verified) on Wed, 2008-05-28 10:18
Thanks for pointing that

Thanks for pointing that out, something must've happened while I was upgrading the site... :P

anyway, they're all back now

Posted by Dan Hakimzadeh on Wed, 2008-05-28 15:21
Adding filesize

In the above code, make the following modifications to include filesize:
(from this page: http://drupal.org/node/86413)

Change:
$rows[] = array(theme('file_icon', $file) ,l($text, $href));
To:
$rows[] = array(theme('file_icon', $file) ,l($text, $href), format_size($file->filesize));

Change:
$header = array(t('Attachment'), t('(click to download)'));
To:
$header = array('', t('Attachment'), t('(click to download)'));

Posted by MikeQue (not verified) on Wed, 2008-05-28 19:31
Thanks! You beat me to

Thanks!
You beat me to it....

Posted by Dan Hakimzadeh on Wed, 2008-05-28 22:11
This doesn't seem to work

This doesn't seem to work using the "clean" theme. Any ideas why?

Clean theme link:
http://drupal.org/project/clean

Posted by krisbfunk (not verified) on Fri, 2008-10-03 12:57
Also I have noticed this is

Also I have noticed this is no longer working if I turn on the Private Upload module. When I turn off the module, the theming returns. I've posted it as an issue here: http://drupal.org/node/337951

Posted by Visitor (not verified) on Sun, 2008-11-23 01:14
much thanks

great code!

Posted by keven (not verified) on Thu, 2008-12-04 20:44
You using Drupal 5? Also

You using Drupal 5? Also make sure you aren't pasting into template.php with the php tags that wrap the whole function...

Posted by Dan Hakimzadeh on Thu, 2009-04-30 11:08
Post new comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <small> <pre> <strike> <sub> <sup> <kbd> <s>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.