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.

CommentFileSizeAuthor
#8 filefield_icon_sets.patch3.73 KBquicksketch
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

brian_c’s picture

Title: possible alternate icon set » possible altenrate icon set

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:


// implementation of theme_filefield_icon(), use our own icon set
function MYTHEME_filefield_icon($file) {
	global $base_url;
	if (is_object($file)) {
		$file = (array) $file;
	}
	$icon_url = $base_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
}

(Edit: I've fixed a typo and incorporated use of $base_url as mentioned in #5 below)

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.

brian_c’s picture

Title: possible altenrate icon set » possible alternate icon set
quicksketch’s picture

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.

brian_c’s picture

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

dogbertdp’s picture

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 "?>"):

/**
 * 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

brian_c’s picture

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.

srobert72’s picture

Subscribe

quicksketch’s picture

Title: possible altenrate icon set » Add ability to use alternative icon sets
FileSize
3.73 KB

I'm moving the topic of this issue to a new direction. Because we're not going to add multiple icon sets to FileField directly, we should provide the ability to have icon packs be provided by other modules. The D7 file.module already provides this functionality through a variable (see http://api.lullabot.com/file_icon_path/7). Unfortunately in D6 we have named themes (maybe that's actually a good thing) not icon directories. To maintain existing API compatibility in D6, we need to keep this $theme concept but allow other modules to provide a file icon set directory. This patch allows this through hook_filefield_icon_sets().

quicksketch’s picture

Version: 6.x-3.x-dev » 6.x-3.5
Status: Active » Fixed

I've committed this patch and it will be in 3.6.

nemchenk’s picture

Am I being daft, or can this only be done by implementing hook_filefield_icon_sets() in a module? Can this be done in template.php?

quicksketch’s picture

Providing options to switch icons and multiple icon sets can only be provided by modules. However you can (and always have been able to) override the display of all FileField icons by overriding theme_filefield_icon().

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

cluke009’s picture

I had issues with both of the above solutions and wrote some new code. This seems a little simpler to me as it only needs to override 1 function.

This just points to my images directory and uses the mimetype as the filename.
ie: application-pdf, application-msword

<?php
// implementation of theme_filefield_icon(), use our own icon set
function MYTHEME_filefield_icon($file) {
  if (is_object($file)) {
    $file = (array) $file;
  }
  $mime = check_plain($file['filemime']);

  $dashed_mime = strtr($mime, array('/' => '-', '+' => '-'));

  if ($icon_url = filefield_icon_url($file)) {
    return '<img class="filefield-icon field-icon-'. $dashed_mime .'"  alt="'. t('@mime icon', array('@mime' => $mime)) .'" src="' . base_path() . path_to_theme() . '/images/' . $dashed_mime .'.png" />';
  }
}
?>