Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
<?php
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;
}
?>
Comments
this code into template.php?
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!
Yes, you would put that in
Yes, you would put that in your template.php.