Drupal 4.7: Hacking the Upload Module

I’ve chosen Drupal for a web site project for a long-time client. We’re slogging our way through hundreds of pages of content, and have decided that we want to display attachment uploads by category, with little to no impact to the content manager uploading the attachments.

The solution/hack involved a customization to the Upload module, replacing the theme_upload_attachments() function with the customized version below. The idea was to have the content manager add a prefix to the filenames that would identify the category, and the software would take care of the rest.

Some notes:

  • [+] It can’t get much easier than adding a prefix to a file before you upload it to the web site.
  • [+] FTP file listings are now sorted by category in the Drupal files/uploads folder.
  • [-] If the content manager makes a mistake in naming a file, she has to remove the upload, rename the file on her file system, and re-upload.

Here’s the modified function for Drupal 4.7, use at your own risk. At some time before we launch the site I’ll upgrade to Drupal 5.1, and post a modified version of the function for 5.1.

/**
* Displays file attachments in table
*/
function theme_upload_attachments($files) {
  $header = array(t('Name'), t('Size'));
  $spec_rows = array();
  $dsgn_rows = array();
  $inst_rows = array();
  $case_rows = array();
  $rows = array();
  $str = array();

  foreach ($files as $file) {
    if ($file->list) {
      $href = $file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()));
      $text = $file->description ? $file->description : $file->filename;

      if (strpos($file->filepath,'spec_') != false) {
        $spec_rows[] = array(l($text, $href), format_size($file->filesize));
      } elseif (strpos($file->filepath,'dsgn_') != false) {
        $dsgn_rows[] = array(l($text, $href), format_size($file->filesize));
      } elseif (strpos($file->filepath,'inst_') != false) {
        $inst_rows[] = array(l($text, $href), format_size($file->filesize));
      } elseif (strpos($file->filepath,'case_') != false) {
        $case_rows[] = array(l($text, $href), format_size($file->filesize));
      } else {
        $rows[] = array(l($text, $href), format_size($file->filesize));
      }
    }
  }

  if (count($spec_rows)) {
    $str[] = '<h2>Product Specifications</h2>';
    $str[] = theme('table', $header, $spec_rows, array('id' => 'attachments'));
  }
  if (count($dsgn_rows)) {
    $str[] = '<h2>Design Information</h2>';
    $str[] = theme('table', $header, $dsgn_rows, array('id' => 'attachments'));
  }
  if (count($inst_rows)) {
    $str[] = '<h2>Installation Guides</h2>';
    $str[] = theme('table', $header, $inst_rows, array('id' => 'attachments'));
  }
  if (count($case_rows)) {
    $str[] = '<h2>Case Studies</h2>';
    $str[] = theme('table', $header, $case_rows, array('id' => 'attachments'));
  }
  if (count($rows)) {
    $str[] = '<h2>Other Downloads</h2>';
    $str[] = theme('table', $header, $rows, array('id' => 'attachments'));
  }
  return implode('',$str);
}

About this entry