Make Drupal attachments list show date, not size

Nobody was interested in the file sizes for the different procedures and memos attached to pages on our site -- they just wanted to know whether they were getting a current version of the document. Luckily, the function that displays the table of attachments is themeable. Add a theme-specific function to template.php in YourTheme directory:

/**
 * Change attachment listings to show updated date rather than file size
 * replaces modules/upload/upload.module, line 352
 * http://api.drupal.org/api/drupal/modules--upload--upload.module/function/theme_upload_attachments/6
 */  

function YourTheme_upload_attachments($files) {
  $header = array(t('Attachment'), ' ', t('Date'));
  $rows = array();
  foreach ($files as $file) {
    $file = (object) $file;
    if ($file->list && empty($file->remove)) {
      $href = file_create_url($file->filepath);
      $text = $file->description ? $file->description : $file->filename;
      $rows[] = array(l($text, $href), ' ', format_date($file->timestamp,'custom','M j, Y'));
    }
  }
  if (count($rows)) {
    return theme('table', $header, $rows, array('id' => 'attachments'));
  }
}

The above uses the column header "Date" and this turns out to present another problem. The /misc/tableheader.js JavaScript does an elaborate calculation of the best width -- in pixels -- for each table cell. With a short word in the column header, the cells below it are all set to an overly narrow width, causing wrapping even on the relatively short date format used in this code (e.g., Dec 23, 2010). To work around that, I also added this to the theme stylesheet:

 #attachments tbody td {
	min-width: 100px;
}
Another option would be to replace the word "Date" with some longer column header like "Last modified" -- thus forcing tableheader.js to calculate a wider column.

Tags: