Add class names to the attachments table cells
Description
The following snippet overrides the upload_attachments theme to provide the opportunity for adding class names to each cell of the table. This is really useful for theming up the attachment table that is displayed by default in nodes.
Step 1 of 2
Add the following code to your template.php file.
<?php
function phptemplate_upload_attachments($files) {
$header = array(
array('data' => t('Attachment'),'class' => 'attachment'),
array('data' => t('Size'), 'class' => 'size')
);
$rows = array();
foreach ($files as $file) {
$file = (object)$file;
if ($file->list && !$file->remove) {
// Generate valid URL for both existing attachments and preview of new attachments (these have 'upload' in fid)
$href = file_create_url((strpos($file->fid, 'upload') === FALSE ? $file->filepath : file_create_filename($file->filename, file_create_path())));
$text = $file->description ? $file->description : $file->filename;
$rows[] = array(
array('data' => l($text, $href), 'class' => 'file'),
array('data' => format_size($file->filesize), 'class' => 'filesize')
);
}
}
if (count($rows)) {
return theme('table', $header, $rows, array('id' => 'attachments'));
}
}
?>Step 2 of 2
Tweak the class names as you require. The class names used in this example are as follows:
- 'class' => 'attachment'
- 'class' => 'size'
- 'class' => 'file'
- 'class' => 'filesize'
That should be it! This is my first post so please excuse any formatting disasters.
