Show node attachments in a block

If you want to show the attachments of a specific node in a block you can use the following code snippets.

Show attachments in a table:

<?php

 
// We load the node and the attached files by the nid (here nid = 26)
 
$files = upload_load(node_load(26));
 
// and use the themeing function of the upload module to print the attachment table
 
print theme_upload_attachments($files);

?>

Show attachments in a table with mime-type column:

<?php

 
// We load the node and the attached files by the nid (here nid = 26)
 
$files = upload_load(node_load(26));
 
$rows = array();
  foreach (
$files as $file) {
    if (
$file->list) {
     
$href = $file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()));
     
$text = $file->description ? $file->description : $file->filename;
     
$mime = strtoupper(substr($file->filemime, strrpos($file->filemime, '/')+1));
     
$rows[] = array(l($text, $href), format_size($file->filesize), $mime);
    }
  }

  if (
count($rows)) {
    print
theme('table', $header, $rows, array('id' => 'attachments'));
  }

?>

Show attachments as list:

<?php

  
// We load the node and the attached files by the nid (here nid = 26)
 
$files = upload_load(node_load(26));
 
$rows = array();
  foreach (
$files as $file) {
    if (
$file->list) {
     
$href = $file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()));
     
$text = $file->description ? $file->description : $file->filename;
     
$mime = strtoupper(substr($file->filemime, strrpos($file->filemime, '/')+1));
     
$rows[] = array(l($text, $href), format_size($file->filesize), $mime);
    }
  }

  foreach(
$rows as $row){
  
$listBullets .= '<li>'.$row[0].' <small>['.$row[2].', '. $row[1].']</small>'.'</li>';
  }

   print
'<ul>'.$listBullets.'</ul>';
?>

I use this code on www.mentavis.com for the download box at the right sidebar.

---------------------------------------------------------------------------
You can contact me over www.creazion.de

Show (current) node attachments in a block

omar - March 28, 2007 - 22:38

The block will appear anytime that a node has attachments. It is based the first of the examples above but sets the $nid automatically.

<?php
if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
 
$nid = (int)arg(1);

 
// We load the node and the attached files by the nid (here nid = $nid)
 
$files = upload_load(node_load($nid));
 
// and use the themeing function of the upload module to print the attachment table
$output theme_upload_attachments($files);
return
$output;
}
?>

Block visibility

Antanins - April 10, 2007 - 17:29

If you want the block to appear only with listed attachments, put the following code into “Show if the following PHP code returns TRUE” text box:

<?php
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
$files = $node->files;
  foreach (
$files as $file) {
    if (
$file->list) {
  return
TRUE;
} else { 
  return
FALSE;
}
}
}
?>

See http://drupal.org/node/124232 for another way round to move the uploaded files table into a block.

--
Antonio Giménez

 
 

Drupal is a registered trademark of Dries Buytaert.