Show node attachments in a block

Last modified: February 21, 2007 - 21:14

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

Block visibility with multiple files.

eloiguell - November 10, 2008 - 14:37

If there is one or more listed attachment. (Fixed snippet, if the first attachment was not listed it didn't show the block).

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

if (
$file_existeix) {
  return
TRUE;
} else { 
  return
FALSE;
}
?>

Thanks for the above code, I

adshill - January 21, 2009 - 00:51

Thanks for the above code, I thought it was working great - but it actually creates the following error when you disable file attachments for a node type:

warning: Invalid argument supplied for foreach() in /home/annalin1/public_html/includes/common.inc(1642) : eval()'d code on line 6.

This is what my watchdog highlighted but I'm not so great at php so I can't really help, but if it helps others not to use unless they can alter or will always have attached files:

Offending code == files; foreach ($files as $file) { if ($file->list) { $file_existeix++; } } } if ($file_existeix) { return TRUE; } else { return FALSE; } ?>

You can also see: http://drupal.org/node/361622

This should work for you

vince.rowe - March 18, 2009 - 12:04

Hi there,

The error you are receiving is because of a small item missing from the above code. It is trying to loop through an array of items (files), but if the $files variable is empty then it is not an array, so the system will correctly display an error.

If you add an array check then it all works great :) Here is the amended code:

<?php
$file_existeix
=0;
if (
arg(0) == 'node' && is_numeric(arg(1))) {
 
$node = node_load(arg(1));
 
$files = $node->files;
  if(
is_array($files)){
    foreach (
$files as $file) {
     if (
$file->list) {

     
$file_existeix++;
      }
    }
  }
}

if (
$file_existeix) {
  return
TRUE;
} else {
  return
FALSE;
}
?>

Disabling original list of attachments

castlebuilder - May 12, 2009 - 22:11

I tried the above and have successfully duplicated the attachments list to a block on the left sidebar that only shows when there are attachments in a node. However, how do I get the attachments in normal placement on bottom of page to NOT appear, so content is not duplicated? I tried disabling attachments for the content type, but this make both lists of attachments disappear. What have I missed?

hacked solution

agerson - June 12, 2009 - 05:16

castlebuilder, this may be a hack, but I was able to prevent the normal attachments table from printing by commenting out this line from upload.module.

<?php
function upload_nodeapi(&$node, $op, $teaser) {
  switch (
$op) {

    case
'load':
     
$output = '';
      if (
variable_get("upload_$node->type", 1) == 1) {
       
$output['files'] = upload_load($node);
       
//return $output;
     
}
?>

Sorry... I only saw this now.

adshill - June 15, 2009 - 21:39

Sorry... I only saw this now. I used the stylesheet and just set the attachments to:

#attachments {
display: none;
}

Which worked for me. Not sure if its more, or less hacky than the other suggestion! But it works :)

Thanks Omar

fideaux - July 9, 2009 - 16:13

+1

 
 

Drupal is a registered trademark of Dries Buytaert.