Listing attached documents in a custom block
Here's how to create a custom block that does the following:
- Hide the default attachments list at the bottom of nodes.
- Add a custom block that automatically shows whenever a node contains attachments.
- Add file type images.
Directions
Create a custom block, and place the following code in the block body field. Be sure to use the PHP Input Filter.
* if (arg(0) == 'node' && is_numeric(arg(1))) {
* $node = node_load(arg(1));
* print theme('upload_attachments', $node->files, true);
* }
Place the following code in template.php
/**
* Displays file attachments in a block
* Replaces default theme in upload.module.
* Moves the display to a block. Must be called within a block using the following code:
* (Include PHP tags!
*
* if (arg(0) == 'node' && is_numeric(arg(1))) {
* $node = node_load(arg(1));
* print theme('upload_attachments', $node->files, true);
* }
*
*/
function [phptemplate]_upload_attachments($files, $in_block = false) {
// NOTE: REPLACE [phptemplete] WITH YOUR THEME NAME!
// Hide the default attachment display at bottom of node pages.
if (!$in_block) {
return;
}
//$header = array(t('File'), t('Size'));
$header = NULL;
$attributes = array(
'id' => 'attachments',
'border' => '0',
'cellspacing' => '0',
'cellpadding' => '0'
);
$rows = array();
foreach ($files as $file) {
$file = (object)$file;
if ($file->list && empty($file->remove)) {
$href = file_create_url($file->filepath);
$mimeimage = '';
$filemime = $file->filemime;
if($filemime == 'application/pdf'){
$mimeimage = '
';
} elseif($filemime == 'image/jpeg'){
$mimeimage = '
';
}
$text = $file->description ? $file->description : $file->filename;
$text = '' . l($text, $href) . '';
// $rows[] = array($text, format_size($file->filesize));
$rows[] = array($mimeimage, $text);
}
}
if (count($rows)) {
$out = theme('table', $header, $rows, $attributes);
$out .= '<div style="clear: both;"><a href="http://www.adobe.com/products/acrobat/readstep2.html" target="_blank">
<img width="112" vspace="3" hspace="3" height="33" src="/sites/default/files/userfiles/get_adobe_reader.gif" alt="" />
</a>';
return $out;
}
}




Comments
Post new comment