Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Drupal - overriding filefield theming

Parent Feed: 

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'.

type == 'publication'){ 
     
      $filepath = $file['filepath'];
      //$filename = $file['filename'];
      $icon = theme('filefield_icon', $file);
      $filesize = '' . format_size($file['filesize']) . '';  
      $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.

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web