Skip to content

Instantly share code, notes, and snippets.

@fieke
Created September 18, 2013 08:29
Show Gist options
  • Save fieke/6606251 to your computer and use it in GitHub Desktop.
Save fieke/6606251 to your computer and use it in GitHub Desktop.
add file size to downloads
//FIELD PREPROCESSING MADE POSSIBLE FOR ANY FIELD
function theme_process_field(&$variables) {
$function = 'autorijschool_process_field__'. $variables['element']['#field_name'];
if(function_exists($function)) {
$variables = $function($variables);
}
}
//ADDS FILESIZE TO A DOCUMENTS FIELD
function theme_process_field__field_documents (&$variables) {
foreach($variables['items'] as $key => $val) {
$val['#file']->filename = $val['#file']->filename . " (" . __format_file_fize_size($val['#file']->filesize) . ")";
}
return $variables;
}
//FORMATS FILESIZE INTO SOMETHING READABLE
function __format_file_fize_size( $size, $display_bytes=false ) {
if( $size < 1024 )
$filesize = $size . ' bytes';
elseif( $size >= 1024 && $size < 1048576 )
$filesize = round( $size/1024, 2 ) . ' KB';
elseif( $size >= 1048576 )
$filesize = round( $size/1048576, 2 ) . ' MB';
if( $size >= 1024 && $display_bytes )
$filesize = $filesize . ' (' . $size . ' bytes)';
return $filesize;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment