How are file attachments added into $content

I am using CCK and Catgeory modules with drupal 4.7 to create a specific page type

but I can't find out how to display details of any attachments a node might have, but if I simply print $content they are included in it

I dont want to print $content because if a weburl field is added to the node using cck, then it is also printed in $content

see these examples

This link uses $content

This link uses the various cck fields without using $content

any suggestions greatfully received

Tkgafs

Comments

tkgafs’s picture

I should have said I'm using the attachment module and the filemanager module

robert.redl@easytouch.cc’s picture

I use this in the CCK template to manually reproduce the attached files

 if ($files){
print '<br><table border="1" cellpadding="5"><tr><td>';
print '<table border="0" cellpadding="2">';
print '<tr><td></td><td><b>Attachment</b></td><td><b>Size</b></td></tr>';

foreach ($files as $file){
 $ext = substr($file->description,-3);
 $ext = strtoupper($ext);
 $icon = 'unknown.gif';
 if ($ext=='BAT'){$icon = 'bat.gif';}
 if ($ext=='BMP'){$icon = 'img.gif';}
 if ($ext=='EXE'){$icon = 'exe.gif';}
 if ($ext=='GIF'){$icon = 'img.gif';}
 if ($ext=='JPG'){$icon = 'img.gif';}
 if ($ext=='PNG'){$icon = 'img.gif';}
 if ($ext=='TIF'){$icon = 'img.gif';}
 if ($ext=='TIFF'){$icon = 'img.gif';}
 if ($ext=='DOC'){$icon = 'doc.gif';}
 if ($ext=='XLS'){$icon = 'xls.gif';}
 if ($ext=='PPT'){$icon = 'ppt.gif';}
 if ($ext=='PDF'){$icon = 'pdf.gif';}
 if ($ext=='GZ'){$icon = 'zip.gif';}
 if ($ext=='TAR'){$icon = 'zip.gif';}
 if ($ext=='RAR'){$icon = 'zip.gif';}
 if ($ext=='TXT'){$icon = 'txt.gif';}
 if ($ext=='ZIP'){$icon = 'zip.gif';}
 $filetype = '<img src="/files/'.$icon.'">';  // path to your filetype icons
 print '<tr>';
 print '<td>'.$filetype.'</td>';
 print '<td><a href="'.base_path().$file->filepath.'">';
 print $file->description;
 print '</a></td>';
 print '<td>'.format_size($file->filesize).'</td>';
 print '</tr>';
}
 print '</table>';
 print '</td></tr></table>';
}
dublin drupaller’s picture

thanks for posting that snippet...v. helpful.

Any chance you have a zip file of the icons?

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

miles28’s picture

Hi,

I've maden a cck table view of attachments nodes, you can see it at http://www.clubslotsevilla.es/clasificaciones . These views have two fileds:

node: title (normal) ------------------------ file: name (with download link)

Do you konw how could I include a icon beside of "file: name" for a certain type of files?

robert.redl@easytouch.cc’s picture

Here is a download link for the icon images:

http://www.easytouch.com/drupal/attachment_icons

pamphile’s picture

Thanks for the excellent code.

Marcel
Wholesale Directory

iandit’s picture

It's a cool snippet, but it actually only applies to the default/core attachment module, not the filemanager/attachment one.

In the latter case you use $attachments instead of $files. The issue I'm having is in regards to the active/n directories that are used by the filemanager to store node specific attachments seperately (so I understand). And the $attachments variable only gives the filename and not the subdirectory the file is in.

Anyway to get around this and get the full file path?

Thanks,

Mike

pamphile’s picture

try using the variable_get function

variable_get('file_directory_path', "files") ;

Marcel
http://www.acmetutorials.com

seanberto’s picture

Thank you all so much for the help with this. Marcel really pointed me in the right direction on this one.

Here's the revised cck template code to show attachments when using the filemanage and attachment modules. I'm not 100% sure that I've written this so that it takes advantage of the filemanager's private file handling. I don't use that feature, so all my attachments are saved in the "active" directory rather than the "working" directory.

It might be a little sloppy, but it works for me in 4.7.4. The real benefit of using some sort of variation on this code is that you can take advantage of the "hidden" file option, as well as use both the title and description fields.

 if ($attachments) {
<div id="attachments">
$show_attachments = false;
foreach ($attachments as $attachment) {
 if (!$attachment['hidden']) { $show_attachments = true; }
}

if ($show_attachments){
 print '<br><table border="1" cellpadding="5"><tr><td>';
 print '<table border="0" cellpadding="2">';
 $col_headers = '<tr><td></td><td><b>Additional Resources</b></td>';
 $show_description = false;
 foreach ($attachments as $attachment) {
  if (!$attachment[hidden] && $attachment['description'] != "") { $show_description = true; }
 }
 if ($show_description) { $col_headers = $col_headers . '<td><b>Description</b></td>'; }
 $col_headers = $col_headers . '<td><b>Size</b></td></tr>';
 print $col_headers;

 foreach ($attachments as $attachment){
  if (!$attachment['hidden']) {
   $ext = substr($attachment['filename'],-3);
   $ext = strtoupper($ext);
   $icon = 'unknown.gif';
   if ($ext=='BAT'){$icon = 'bat.gif';}
   if ($ext=='BMP'){$icon = 'img.gif';}
   if ($ext=='EXE'){$icon = 'exe.gif';}
   if ($ext=='GIF'){$icon = 'img.gif';}
   if ($ext=='JPG'){$icon = 'img.gif';}
   if ($ext=='PNG'){$icon = 'img.gif';}
   if ($ext=='TIF'){$icon = 'img.gif';}
   if ($ext=='TIFF'){$icon = 'img.gif';}
   if ($ext=='DOC'){$icon = 'doc.gif';}
   if ($ext=='XLS'){$icon = 'xls.gif';}
   if ($ext=='PPT'){$icon = 'ppt.gif';}
   if ($ext=='PDF'){$icon = 'pdf.gif';}
   if ($ext=='GZ'){$icon = 'zip.gif';}
   if ($ext=='TAR'){$icon = 'zip.gif';}
   if ($ext=='RAR'){$icon = 'zip.gif';}
   if ($ext=='TXT'){$icon = 'txt.gif';}
   if ($ext=='ZIP'){$icon = 'zip.gif';}
   $filetype = '<img src="/files/attachment_icons/'.$icon.'">';  // path to your filetype icons
   print '<tr>';
   print '<td>'.$filetype.'</td>';
   $file = module_invoke('filemanager', 'get_file_info', $attachment['fid']);
   print '<td><a href="'.str_replace('&amp;', '&', module_invoke('filemanager', 'url', $file, FALSE)).'">';
   print $attachment['title'];
   print '</a></td>';
   if ($attachment['description'] != "") {
    print '<td>'.$attachment['description'].'</td>';
   }
   print '<td>'.format_size($attachment['size']).'</td>';
   print '</tr>';
  }
 }
 print '</table>';
 print '</td></tr></table>';
}
</div>
}

A couple of notes:
1. I added the div tag so that I could customize the CSS. It's not necessary.
2. This code checks to see if any of the attached files are NOT selected as hidden. If all are hidden, the table does not show up in the node body.
2. I added a conditional statement that checks to see if the description field is empty for all non-hidden attachments. If all are empty, it skips this column in the table and does not print the "description" header.

Follow-up:
It would be great to add "odd/even" tags, and maybe play with the table headers a bit more. But it does what I need at the moment.

Cheers,
Sean

Sean Larkin

Axel_V’s picture

Hi Sean,
very, very nice piece of code. Exactly what I need. What do I have to do to print it the other way round so that the newest attachment is always on top. Probably easy but my PhP is very meagre. Any help would be very much appreciated.
cheers, xl

sakib000’s picture

In 5.x (must be there in 4.x too), <div id="attachments"> is causing fatal error, remove it and also remove </div> on third last line

Use this instead

<?php if ($attachment): ?>
<div class="attachments">




<?php
if ($attachments) {
$show_attachments = false;
foreach ($attachments as $attachment) {
if (!$attachment['hidden']) { $show_attachments = true; }
}

if ($show_attachments){
print '<br><table border="1" cellpadding="5"><tr><td>';
print '<table border="0" cellpadding="2">';
$col_headers = '<tr><td></td><td><b>Additional Resources</b></td>';
$show_description = false;
foreach ($attachments as $attachment) {
  if (!$attachment[hidden] && $attachment['description'] != "") { $show_description = true; }
}
if ($show_description) { $col_headers = $col_headers . '<td><b>Description</b></td>'; }
$col_headers = $col_headers . '<td><b>Size</b></td></tr>';
print $col_headers;

foreach ($attachments as $attachment){
  if (!$attachment['hidden']) {
   $ext = substr($attachment['filename'],-3);
   $ext = strtoupper($ext);
   $icon = 'unknown.gif';
   if ($ext=='BAT'){$icon = 'bat.gif';}
   if ($ext=='BMP'){$icon = 'img.gif';}
   if ($ext=='EXE'){$icon = 'exe.gif';}
   if ($ext=='GIF'){$icon = 'img.gif';}
   if ($ext=='JPG'){$icon = 'img.gif';}
   if ($ext=='PNG'){$icon = 'img.gif';}
   if ($ext=='TIF'){$icon = 'img.gif';}
   if ($ext=='TIFF'){$icon = 'img.gif';}
   if ($ext=='DOC'){$icon = 'doc.gif';}
   if ($ext=='XLS'){$icon = 'xls.gif';}
   if ($ext=='PPT'){$icon = 'ppt.gif';}
   if ($ext=='PDF'){$icon = 'pdf.gif';}
   if ($ext=='GZ'){$icon = 'zip.gif';}
   if ($ext=='TAR'){$icon = 'zip.gif';}
   if ($ext=='RAR'){$icon = 'zip.gif';}
   if ($ext=='TXT'){$icon = 'txt.gif';}
   if ($ext=='ZIP'){$icon = 'zip.gif';}
   $filetype = '<img src="/files/attachment_icons/'.$icon.'">';  // path to your filetype icons
   print '<tr>';
   print '<td>'.$filetype.'</td>';
   $file = module_invoke('filemanager', 'get_file_info', $attachment['fid']);
   print '<td><a href="'.str_replace('&amp;', '&', module_invoke('filemanager', 'url', $file, FALSE)).'">';
   print $attachment['title'];
   print '</a></td>';
   if ($attachment['description'] != "") {
    print '<td>'.$attachment['description'].'</td>';
   }
   print '<td>'.format_size($attachment['size']).'</td>';
   print '</tr>';
  }
}
print '</table>';
print '</td></tr></table>';
}
}
?>




</div>
<?php endif; ?>