Last updated April 1, 2009. Created on April 1, 2009.
Edited by advseb. Log in to edit this page.

Sometimes it is important to theme a specific filefield and not all file fields in general. However, filefield only provides formatter functions applying to all files. Therefore, you have to select in the formatter function the specific field you want to format. The following code shows how to do that for the default formatter. Note that for images and non generic files other formatter functions are used and must be overwritten.

function [theme_name]_filefield_formatter_default($element) {
  $file = $element['#item'];
  $field = content_fields($element['#field_name']);

  if ($field['field_name'] == 'field_image') {
    $output = 'Special output just for the field_image field';

  $output .= theme('filefield_item', $file, $field);
  return $output;

Looking for support? Visit the forums, or join #drupal-support in IRC.


TommyK’s picture

I'm new to theming, and don't really know PHP. I know just enough to play around and modify things here and there. I just want to be sure about this:

Do I put the above code into the template.php file of my theme?

Also, which pieces of the above code would I need to replace with my own field names and other bits? I understand the [theme_name] one, but what about the things like #item, #field_name, field_image, and fieldfield_item? Would I have customized terms for those pieces?

Thanks so much!

advseb’s picture

Yes, you would put that in your template.php.