Community Documentation

Adding icons to attachment display

Last updated October 18, 2011. Created by Minnov on September 28, 2006.
Edited by MGParisi, SLIU, jhodgdon, bryan kennedy. Log in to edit this page.

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

Alternative methods

Comments

this module do that iTweak Upload

iTweak Upload

http://drupal.org/project/itweak_upload

saludos desde Colombia

ingenieria de sistemas

Page status

Needs updating

Log in to edit this page

About this page

Drupal version
Drupal 4.7.x
Audience
Developers and coders, Themers

Archive

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here