Posted By

zachharkey on 02/04/09


Tagged

drupal theme upload-attachments-tpl-php


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

Onfire60
halfshadows
Alwaison
rhainek


Theme the Drupal attachments table


 / Published in: PHP
 

Create theme override file: upload_attachments.tpl.php in your theme directory.

CSS styles

  1. <?php
  2. /**
  3.  * Themed attachments table
  4.  */
  5. $header = array(t('File name'), t('Size'));
  6. $rows = array();
  7. foreach ($files as $file) {
  8. if ($file->list) {
  9. $type = explode('/', $file->filemime);
  10. // Quick fix. Should use some kind of make_safe_for_css() function.
  11. $type = str_replace('.', '-'. $type[1]);
  12. $href = check_url(($file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()))));
  13. /* To do:
  14.   if (!empty($file->description)) {
  15.   $text = check_plain($file->description) . '<br /><span class="filename">'.$file->filename.'</span>';
  16.   }
  17.   else {
  18.   $text = check_plain($file->filename);
  19.   }
  20.   */
  21. $text = check_plain($file->description ? $file->description : $file->filename);
  22. // Because the drupal format_size function doesn't round far enough to be smooth.
  23. $size = $file->filesize;
  24. $suffix = t('bytes');
  25. if ($size >= 1024) {
  26. $size = round($size / 1024, 0);
  27. $suffix = t('KB');
  28. }
  29. if ($size >= 1024) {
  30. $size = round($size / 1024, 0);
  31. $suffix = t('MB');
  32. }
  33.  
  34. $size = array('data' => t('%size %suffix', array('%size' => $size, '%suffix' => $suffix)), 'class' => 'size');
  35.  
  36. if ($type == 'jpeg' ) {
  37. $text = array('data' => l($text, $href, array('title' => 'Right click to download', 'class' => 'thickbox'), $query = NULL, $fragment = NULL, $absolute = FALSE, $html = TRUE), 'class' => $type);
  38. }
  39. else {
  40. $text = array('data' => l($text, $href, array('title' => 'Right click to download'), $query = NULL, $fragment = NULL, $absolute = FALSE, $html = TRUE), 'class' => $type);
  41. }
  42. $rows[] = array($text, $size);
  43. }
  44. }
  45.  
  46. if (count($rows)) {
  47. print '<div class="attachments">'. theme('table', $header, $rows, array('class' => 'attachments'), 'Downloadable files') .'</div>';
  48. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: kurokikaze on February 5, 2009

Drupal 5 or 6?

Posted By: zachharkey on March 16, 2009

Drupal 6. But it will work in Drupal 5 if you put a callback stub in template.php.

Posted By: Alwaison on August 29, 2011

Very useful. Thanks!

You need to login to post a comment.