Community Documentation

Add class names to the attachments table cells

Last updated August 23, 2009. Created by kjay on May 24, 2008.
Edited by SLIU. Log in to edit this page.

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.

About this page

Drupal version
Drupal 5.x

Theming Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.