7.x file.module theme_file_icon($variables)

Returns HTML for an image with an appropriate icon for the given file.


$variables: An associative array containing:

  • file: A file object for which to make an icon.
  • icon_directory: (optional) A path to a directory of icons to be used for files. Defaults to the value of the "file_icon_directory" variable.
  • alt: (optional) The alternative text to represent the icon in text-based browsers. Defaults to an empty string.

Related topics

1 theme call to theme_file_icon()
theme_file_link in modules/file/file.module
Returns HTML for a link to a file.


modules/file/file.module, line 841
Defines a "managed_file" Form API field and a "file" field for Field module.


function theme_file_icon($variables) {
  $file = $variables['file'];
  $alt = $variables['alt'];
  $icon_directory = $variables['icon_directory'];
  $mime = check_plain($file->filemime);
  $icon_url = file_icon_url($file, $icon_directory);
  return '<img class="file-icon" alt="' . check_plain($alt) . '" title="' . $mime . '" src="' . $icon_url . '" />';


ladybug_3777’s picture

When working with file icons I noticed 2 annoying things.
1) The file icons do not include alt or title tags
2) The src is a relative URL instead of a fully qualified URL so if you have a file icon being displayed in an RSS feed, when that RSS feed is displayed in someone's email the icon is broken because it does not include the domain name.

Here is a quick fix that can be used to address both of those issues:

function yourthemename_file_icon($variables) {
$file = $variables['file'];
$icon_directory = $variables['icon_directory'];
$mime = check_plain($file->filemime);
$icon_url = file_icon_url($file, $icon_directory);
return '';

Hope this helps someone looking at this hook and wondering how that might be done :-)

maxplus’s picture

Thanks @ladybug_3777,

I had the same problem with the Message module where the link to to file type icon was also not displayed (because of the path to the icon is /modules/...)

Now the file link is displayed without an icon, perfect!