by default "table of files" displays format in cck file field display two columns>
1. file name
2. file siz
i wanna show description field for each file in a column next to file size column.
file.field.inc in file module has something like this:

function theme_file_formatter_table($variables) {
  $header = array(t('Attachment'), t('Size'),t('<strong>my needed desc field</strong>');
  $rows = array();
  foreach ($variables['items'] as $delta => $item) {
    $rows[] = array(
      theme('file_link', array('file' => (object) $item)),
      format_size($item['filesize'])
    );
  }

  return empty($rows) ? '' : theme('table', array('header' => $header, 'rows' => $rows));
}

i added 'my needed desc field' to above code(in $header) but i don`t know how to show description field of each file in $rows. is there anyway to do that?

Comments

rar’s picture

In your theme put the function MYTHEME_file_formatter_table
in your template.php to override . Instead of MYTHEME you will put the name of your theme.

Here's an example which adds upload date.

function MYTHEME_file_formatter_table($variables) {
  $header = array(t('Attachment'), t('Size'),t('Upload Date'));
  $rows = array();
  foreach ($variables['items'] as $delta => $item) {
    if ($item['timestamp'] == 0) {  
      $item['timestamp'] = 1234567890;  //or whatever you want for the default date
    }  
    $rows[] = array(
      theme('file_link', array('file' => (object) $item)),
      format_size($item['filesize']),
      format_date($item['timestamp'],'custom','m/d/Y'),
    );
  }

  asort($rows);
  return empty($rows) ? '' : theme('table', array('header' => $header, 'rows' => $rows));
}
openstep’s picture

Hello,

I have succesfully added the timestamp as described above but heaving problems adding a description.
Can you please tell me what info and where should I look for to insert?

I have looked file types under structure/file type/document/manage fields and have found what I previously added file description with the machine name: field_leiras.
I added this to the file.field.inc under format_date but when I refresh nothing shows.

Where should I look for the name the I need to enter to get the description in table of files?

thx

DRIVE’s picture

When I set up content types with file attachments I usually select "allow title". By default, if no title is entered the filename is used by default. So, below are just a couple of items I've used to make some small tweaks to the output. You can further tweak each item with CSS; for example, setting "file table" text to 10px and after that rule setting a rule for file-table LINKS to be 11px or whatever. Works like a charm.

// TWEAK FILE ATTACHMENT TABLE WITH FILE-TITLE (linked) FILESIZE & DATE
// Replace theme name with your theme. Replace "XXCUSTOMfilesize" with anything you like
function YOUR_THEME_NAME_file_formatter_table($variables) {
  $header = array(t('Attachment'), t('Size'), t('Date'));
  $rows = array();
  foreach ($variables['items'] as $delta => $item) {
      $XXCUSTOMfilesize = format_size($item['filesize']);
    $rows[] = array(
      theme('file_link', array('file' => (object) $item)),
      //format_size($item['filesize']);
      format_size(round($XXCUSTOMfilesize)), // removes decimals;see PHP round for options
      //format_date($item['timestamp'], $type = 'custom', 'D, n/j/Y g:ia T'), includes users timezonne (EST, PST etc)
      format_date($item['timestamp'], $type = 'custom', 'D, n/j/Y g:ia'),
      );
  }
  return empty($rows) ? '' : theme('table', array('header' => $header, 'rows' => $rows));
}


// FILE ATTACHMENTS With Titles
function YOUR_THEME_NAME_file_link($variables) {
  $file = $variables['file'];
  $icon_directory = $variables['icon_directory'];
 
  $url = file_create_url($file->uri);
  $icon = theme('file_icon', array('file' => $file, 'icon_directory' => $icon_directory));
 
  // Set options as per anchor format described at
  // http://microformats.org/wiki/file-format-examples
  $options = array(
    'attributes' => array(
      'type' => $file->filemime . '; length=' . $file->filesize,
    ),
  );
 
  // Use the description as the link text if available.
  if (empty($file->description)) {
    $link_text = $file->filename;
  }
  else {
    $link_text = $file->description;
    // $options['attributes']['title'] = check_plain($file->filename);
    $options['attributes']['title'] = check_plain($file->description . ' (' . $file->filemime . ')');
  }

~enjoy or loss of nothing :)
From These Ruins - Temptation