Drupal - overriding filefield theming

Drupal's default 'Generic File' format can be a bit ugly. Luckily, it's not too hard to override. If you'd like to change the default filefield theming for 'Generic File', try using the theme_filefield_item() function in your template.php file.

Note: You may also be interested in checking out my post on customizing the filefield format in views.

In the example below, I overrode the default theming for all cck fieldfields belonging to node type 'publication'.

function grasmash_filefield_item($file, $field) {
  if (filefield_view_access($field['field_name']) && filefield_file_listed($file, $field)) {
    $node = node_load($file['nid']);
    if ($node->type == 'publication'){ 
      $filepath = $file['filepath'];
      //$filename = $file['filename'];
      $icon = theme('filefield_icon', $file);
      $filesize = '<span class="file-size">' . format_size($file['filesize']) . '</span>';  
      $link = l(t('Download Related Publication'), $filepath, array('attributes' => array('class' => 'download-publication')));
      return $icon . $link . ' ' . $filesize;
    else {
      return theme('filefield_file', $file); //default theming
  return '';

One downside to this particular implementation is that I'm calling node_load every time that a file is themed. This makes a database call for each file, which can cause performance issues if you're loading too many. Just something to keep in mind.

If you are loading the node for that page anyway, you should be fine because node_load has a static variable that holds the node.


No extra database hit unless you are loading nodes on a page that wasn't going to load them in the first place.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.