Listing attached documents in a custom block

Printer-friendly version

Here's how to create a custom block that does the following:

  1. Hide the default attachments list at the bottom of nodes.
  2. Add a custom block that automatically shows whenever a node contains attachments.
  3. 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

  • Allowed HTML tags: <a> <div> <em> <h1> <h2> <h3> <h4> <h5> <cite> <code> <img> <ul> <ol> <li> <dl> <dt> <dd> <p> <strong> <strike> <table> <tr> <td> <tbody>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.

Creative Commons License
ronliskey.com is licensed under a Creative Commons License.