possible alternate icon set

pwolanin - May 4, 2009 - 00:45
Project:FileField
Version:6.x-3.x-dev
Component:User interface
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

I just came across this icon set today: http://www.splitbrain.org/projects/file_icons

Wondering if it would be a better alternative to what's used now and or they could be used in addition? Seems like there are more and more specific icons.

#1

brian_c - July 21, 2009 - 02:07

This is fantastic, thanks.

One big problem with filefield's current default icons is they are PNG24's with alpha channel, which look bad in IE6 unless you use some sort of PNGfix technique... which IMHO is a poor solution for the default icon set. These alternate icons are PNG8 so there's no problem with them. They also look closer to standard Windows icons which I think will be more recognizable to the majority of users.

We've copied these into an "images/fileicons" folder in our theme directory, and are using them by way of the following code in our template.php file:

<?php
// implementation of theme_filefield_icon(), use our own icon set
function MYTHEME_filefield_icon($file) {
    if (
is_object($file)) {
       
$file = (array) $file;
    }
   
$icon_url = MYTHEME_icon_url( $file ){
   
$mime = check_plain($file['filemime']);
   
$dashed_mime = strtr($mime, array('/' => '-'));
   
$icon = '<img class="field-icon-'. $dashed_mime .'"  alt="'. $mime .' icon" src="'. $icon_url .'" />';
    return
'<div class="filefield-icon field-icon-'. $dashed_mime .'">'. $icon .'</div>';
}

// return a URL for an icon matching this file's type
function MYTHEME_icon_url( $file ){
   
$ext = strtolower( pathinfo( $file['filename'], PATHINFO_EXTENSION ) );
   
$dir = path_to_theme() . '/images/fileicons/';
   
$icon = $dir . $ext . '.png';
    return
file_exists( $icon ) ? $icon : ( $dir . 'file.png' ); // use generic "file" icon if unable to match extension
}
?>

Just change MYTHEME to the name of your theme (and change '/images/fileicons/' if you're storing them somewhere else).

Please note that this uses simple filename extension matching to determine the icon to use, instead of mime-type like filefield's default implementation. This was sufficient for our purposes and will probably be easier for the client to understand if they want to replace any. That being said, it would be straightforward to extend this to check the $file array's mimetype key and use that instead, but you'd have to write a big mapping table. :)

Hope this helps someone.

#2

brian_c - July 21, 2009 - 02:08
Title:possible altenrate icon set» possible alternate icon set

#3

quicksketch - July 21, 2009 - 04:01

Yeah the current way icon sets are set up is sub-optimal. Right now it's practically required to place the icons inside of the FileField module, when it should just be a variable that let's you place the icons anywhere you want as long as they're named properly. I'd be happy to fix this limitation, but I'm not sure how necessary it is to include multiple sets of icons within FileField by default.

#4

brian_c - July 21, 2009 - 15:27

I'd humbly offer two suggestions:

1) As you mentioned, add a textfield in Filefield settings to allow user to specify path to custom icon set, then a non-technical user should be able to replace them by just using the correct filenames

2) Add a theming hook to allow themers to override JUST the function which provides the URL for the icon, given the $file array. This would allow unlimited flexibility for more advanced users, and means they don't have to duplicate the module's output markup like I've done above.

Cheers

#5

dogbertdp - October 2, 2009 - 14:38

brian_c,
Thanks for the code snippet and path. While it's dependent on a file extension, and that may bother some, I think it rather efficient and simple.

That said I couldn't get it to work without a couple of changes. Here is the code I ended up putting in my template.php with comments explaining the changes I made and why (as usually, anyone else using this code should exclude the opening "<?php" and closing "?>"):

<?php
/**
* implementation of theme_filefield_icon(), use our own icon set
* Source: drupal.org/node/452634
*/
function phptemplate_filefield_icon($file) {
   
// **Without using $base_url, my icons tried to load from the "path" of the node.  Using $base_url is the same
    // **way FileField handles this issue in filefield.theme.inc
   
global $base_url;

    if (
is_object($file)) {
       
$file = (array) $file;
    }
   
// **Here I use the global variable $base_url.  Also, there was a typo that caused a fatal exception--
    // **the example in #1 ends the following line with an opening brace and not a semi-colon
   
$icon_url = $base_url .'/'. phptemplate_icon_url( $file );
   
$mime = check_plain($file['filemime']);
   
$dashed_mime = strtr($mime, array('/' => '-'));
   
$icon = '<img class="field-icon-'. $dashed_mime .'"  alt="'. $mime .' icon" src="'. $icon_url .'" />';
    return
'<div class="filefield-icon field-icon-'. $dashed_mime .'">'. $icon .'</div>';
}

// return a URL for an icon matching this file's type
function phptemplate_icon_url( $file ){
   
$ext = strtolower( pathinfo( $file['filename'], PATHINFO_EXTENSION ) );
   
$dir = path_to_theme() . '/images/fileicons/';
   
$icon = $dir . $ext . '.png';
    return
file_exists( $icon ) ? $icon : ( $dir . 'file.png' ); // use generic "file" icon if unable to match extension
}
?>

Thanks for the help,
Mike Hays

#6

brian_c - October 7, 2009 - 23:11

Good point, I forgot that the site I wrote this code for used a <BASE> tag, eliminating the need for us to use $base_url.

 
 

Drupal is a registered trademark of Dries Buytaert.