3

Is there any way in Drupal 7 to display the attachment's file size of a node through the manage file / display of the file field, or is this programmatically achieved?

1
  • Are you using the media module with file entity?
    – Cristian
    Jul 7, 2014 at 8:45

2 Answers 2

4

You can do this hardcoded with the $node object by doing so (preferably in theme_preprocess_node in template.php) :

If you field field_my_field as a single value :

$file = field_get_items('node', $node, 'field_my_file');
if ($file) {
    $file_size = $file[0]['filesize'];
    $file_size_formated = format_size($file_size);
}

If you field field_my_field as a multiple value :

$files = field_get_items('node', $node, 'field_my_file');
if ($files) {
    $files_size_formated = array();
    foreach ($files as $key => $file) {
        $file_size = $file['filesize'];
        $files_size_formated[] = format_size($file_size);
    }
}

In template.php you should have something like this :

// This function allow one preprocess_node function per content type
function mytheme_preprocess_node(&$vars) {
  $node = &$vars['node'];
  // Preprocess function for each node type
  $function = __FUNCTION__ . '_' . $node->type;
  if (function_exists($function)) {
    $function($vars);
  }
}

function mytheme_preprocess_node_my_content_type(&$vars) {
  $node = &$vars['node'];
  $file = field_get_items('node', $node, 'field_my_file');
  if ($file) {
      $file_size = $file[0]['filesize'];
      $vars['file_size_formated'] = format_size($file_size);
  }
}

Then in you node--my-content-type.tpl.php you can print your var $file_size_formated where you want like this for example :

<div class="file-size"><?php print $file_size_formated; ?></div>
4
  • Ok thanks, so in the template.php I should have: function cs_emcdda_preprocess_node(&$variables) { $file = field_get_items('node', $node, 'field_pub_file'); if ($file) { $file_size = $file[0]['filesize']; $file_size_formated = format_size($file_size); } } and in my custom node template <div class="attachments"> <?php print render($content['field_attachment']); ?> </div>
    – Tom Vader
    Jul 7, 2014 at 9:25
  • You need to get $node, for that add $node = &$vars['node'] at the beginning of your function. Also add a condition on $node->type == 'your_content_type'
    – pbonnefoi
    Jul 7, 2014 at 9:27
  • Ok, its working great, thank you so much fo your help @pbonnefoi, the website does not allow me vote in your answer, as soon as its possible, you have my vote :)
    – Tom Vader
    Jul 7, 2014 at 9:42
  • I think you can mark the answer as the right answer for now (it will give you some reputation I think ;-))
    – pbonnefoi
    Jul 7, 2014 at 9:46
1

You can also modify the theme function theme_file_link in your template.php to add the file size to link with something like this:

function YOURTHEME_file_link($variables) {
  // ...

  $file_size = format_size($file->filesize);

  return '<div class="file">' . $icon . ' ' . l($link_text, $url, $options) . '<span class="file-size">' . $file_size . '</span></div>';
}
1
  • This is better and clearer than the accepted answer.
    – Elin Y.
    Dec 28, 2015 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.