User login

Theming the File Attachments in Drupal 5.x

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...

Searched words: 
theming drupal attachment file mimetype icons
File: 
File: 
File: 
File: 
File: 
File: 

Comments

+1

very well written. :-)

loved the comment in the code

otherwise, no point, right

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.

Where are the attached files?

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

Thanks

Amnon

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

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)'));

Thanks! You beat me to

Thanks!
You beat me to it....

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

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

much thanks

great code!

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...

icons in tables

do you know how I could do this for the taxonomy/term, or other table-based views?

file path

I would like to add at this show the file path inside ~ but I do not know how add?
Ask for help ~ ~

<?php
// $Id: webfm_theme.inc,v 1.4 2009/02/08 06:30:30 robmilne Exp $
/**
* Displays file attachments in table
*/

function theme_webfm_attachments($files) {
global $base_url;
$header = array(t('Attachment'));
if($enable_date = variable_get('webfm_attach_date', '')) {
array_push($header, t('Date'));
}
if($enable_size = variable_get('webfm_attach_size', '')) {
array_push($header, t('Size'));
}
$rows = array();
foreach ($files as $file) {
// 0 =inline : 1 = attach
$icon_path = $base_url.'/'.variable_get('webfm_icon_dir', drupal_get_path('module', 'webfm').'/image/icon').'/'._webfm_get_icon($file->e);
$description = '';
if(variable_get('webfm_attach_desc', '') && !empty($file->fdesc)) {
$description = '

'.$file->fdesc.'

';
}
$filename = $file->ftitle ? $file->ftitle : $file->n;
if(variable_get('webfm_attach_new_window', '')) {
$href = array(
'data' => l('[file] ', 'webfm_send/'.$file->id.'/1',
array('attributes' => array('title' => 'Download '.$filename,'target' => '_blank'), 'html' => TRUE))
.l($filename, 'webfm_send/'.$file->id, array('attributes' => array('title' => 'Open '.$filename, 'target' => '_blank')))
.$description, 'class' => 'att-title'
);
} else {
$href = array(
'data' => l('[file] ', 'webfm_send/'.$file->id.'/1',
array('attributes' => array('title' => 'Download '.$filename), 'html' => TRUE))
.l($filename, 'webfm_send/'.$file->id, array('attributes' => array('title' => 'Open '.$filename)))
.$description, 'class' => 'att-title'
);
}

$row = array();
array_push($row, $href);
if($enable_date) {
$time = $file->fcreatedate ? date(webfm_get_date_format(), $file->fcreatedate) : date(webfm_get_date_format(), @filemtime($file->p . '/'. $file->n));
array_push($row, array('data' => $time, 'class' => 'att-time'));
}
if($enable_size) {
array_push($row, array('data' => format_size($file->s), 'class' => 'att-size'));
}
array_push($rows, $row);

}
if (count($rows)) {
return theme('table', $header, $rows, array('class' => 'webfm-attach-list'));
}
}

license question on attached icons

Can I ask if the attached icons are restricted by any licenses, or free for use?

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.