Removing Drupal 7's File Type Icons


September 18th, 2013
by Chris

Drupal 7 has an interesting feature with the core File module, it will automatically look at the mime type of the file and display an appropriate file icon next to a link to the file. This is pretty nice, as you get a free visual indication of what type of file is being linked to. They even provide methods for passing along custom icon sets, so the icons are garaunteed to match the look and feel of your site.

However, say you or your client are interested in not having any icons available. I thought there may be a way to control this through the Drupal admin interface, say under the File Entity 'manage display' section, but I was unable to find any such configuration setting. I was also unable to find a module that would enable an admin configuration setting either. Searching for clues to this was actually a little difficult via Google, as most links were people enquiring about issues with icons not showing up for some reason or another.

So I dug into the File module code to see what options were available.  It turns out the File module provides a theme hook, theme_file_link(), which controls the display of the file, and is subsequently where the icon is added to the display. There are additional hooks you can look at to control the icon display, but this appears to be the root function that will allow you to simply strip out the icons.

The function normally looks like this:

function theme_file_link($variables) {
 $file = $variables['file'];
  $icon_directory = $variables['icon_directory'];

  $url = file_create_url($file->uri);
  $icon = theme('file_icon', array('file' => $file, 'icon_directory' => $icon_directory));

  $options = array(
    'attributes' => array(
       'type' => $file->filemime . '; length=' . $file->filesize,
     ),
  );

  if (empty($file->description)) {
    $link_text = $file->filename;
  } else {
    $link_text = $file->description;
    $options['attributes']['title'] = check_plain($file->filename);
  }

  return '<span class="file"' . $icon . ' ' . l($link_text, $url, $options) . '</span>';
}

The elements highlighted above are all related to the icons, and not needed if you want to disable the file type icons. You override this function in your theme's template.php file. Or you can override it in a custom module so that it's always applied to your site, regardless of the active theme. 

When you override the function, you'll rename the function to either MYTHEME_file_link, or MYMODULE_file_link, depending on which option you're utilizing. Then, simply copy the content of the function, and remove the indicated elements and voilà, the icons will now be gone. You may need to clear the site's cache before the site utilizes the new code though.

Posted in drupal, file, icons, remove



Categories